Thanks for all your answers. I do understand the use of global keyword, and
such cases can be solved by specifying the global keyword.

But its kind of intriguing. In the 1st case the interpreter does not find x
in the local name space but finds it in the enclosing namespace. The same
argument should hold true for the 2nd case. I am not assigning a new value
to x but only printing it. It should have found the x in the enclosing
namespace. I would like to understand this from the interpreter's view.
Python being a dynamically interpreted language, which in simple terms
would mean interpreting each line of code. I was trying to understand how
the interpreter behaves when it encounters the def keyword and how it
assigns namespaces.

Regards

Ranjith
On 02-Jul-2016 9:32 PM, <chennaipy-requ...@python.org> wrote:

Send Chennaipy mailing list submissions to
        chennaipy@python.org

To subscribe or unsubscribe via the World Wide Web, visit
        https://mail.python.org/mailman/listinfo/chennaipy
or, via email, send a message with subject or body 'help' to
        chennaipy-requ...@python.org

You can reach the person managing the list at
        chennaipy-ow...@python.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Chennaipy digest..."


Today's Topics:

   1. Python interpreter (venkata krishnan)
   2. Re: Python interpreter (Sharmila Gopirajan)


----------------------------------------------------------------------

Message: 1
Date: Fri, 1 Jul 2016 17:02:20 +0000 (UTC)
From: venkata krishnan <ve_kr...@yahoo.com>
To: "chennaipy@python.org" <chennaipy@python.org>
Subject: [Chennaipy] Python interpreter
Message-ID:
        <1219998238.455774.1467392540402.javamail.ya...@mail.yahoo.com>
Content-Type: text/plain; charset="utf-8"

Dear Ranjit,
The interpreter is not confused. It actually points out the ambiguity in
that code/intention.An variable cannot be a global and local within that
function's scope. In this scenario you have to use theglobal keyword.
Please check the topic [Global vs. Local Variables and Namespaces?|?here]



|
|   |
Python3 Tutorial: Global vs. Local Variables and Namespaces
 Global versus local Variables, i.e. when and how to use global and local
variables in Python namespaces.  |  |

  |


?ThanksVenkat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <
http://mail.python.org/pipermail/chennaipy/attachments/20160701/9fa3abfa/attachment-0001.html
>

------------------------------

Message: 2
Date: Fri, 1 Jul 2016 23:12:03 +0530
From: Sharmila Gopirajan <sharmila.gopira...@gmail.com>
To: chennaipy@python.org
Subject: Re: [Chennaipy] Python interpreter
Message-ID:
        <CAGzTpACWAxgDRk22=Lp3=+5fqjxkveds9vrzk0wux-7g8ww...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

the variable 'x' defined outside the scope of the function f is a global
variable. Python allows access of the global variable within the function.

But when you assign the value x=5, it starts to treat x as a local
variable. When there is a print statment before the assignment, you get
unbound local error.

x = 1

def f():
      x = 5
      print (x)

f ()  #prints 5
print (x) #prints 1

In the above case, there is a global variable x and there is also a local
variable x. Assigning to the local variable, does not change the global
variable x.

If you do want to modify the global variable, you need to explicitly tell
the interpreter that you would like to work with the global variable.


x = 1

def f():
      global x
       x = 5
      print (x)

f ()  #prints 5
print (x) #prints 5


Date: Fri, 1 Jul 2016 16:57:48 +0530

> From: ranjith pillay <ammaranj...@gmail.com>
> To: chennaipy@python.org
> Subject: [Chennaipy] Python interpreter
> Message-ID:
>         <CABYszAQvLewo8qj_dBr5iP=
> j5v4pk3iatdyjsusafhnpvu_...@mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Hello friends,
>
> I have a question to ask.
>
> If you define the following cases:
>
> Case 1:
> -----------
> x = 1
> def f():
>     print(x)
>
> Case 2:
> -----------
> x = 1
> def f():
>     print(x)
>     x = 5
>     print(x)
>
>
> If you call the function f, in case 1 there won't be a problem, It will
> print 1. But in case 2, it will give an error "UnboundLocalError: local
> variable 'x' referenced before assignment"...One would think that in case
> 2, it should have printed 1 and 5. Any one could explain what is happening
> here? Why does the interpreter get confused in the 2nd Case?
>
> Thanks.
>
> Regards,
> Ranjith
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <
>
http://mail.python.org/pipermail/chennaipy/attachments/20160701/8073650d/attachment-0001.html
> >
>
> ------------------------------
>
> Subject: Digest Footer
>
> _______________________________________________
> Chennaipy mailing list
> Chennaipy@python.org
> https://mail.python.org/mailman/listinfo/chennaipy
>
>
> ------------------------------
>
> End of Chennaipy Digest, Vol 35, Issue 1
> ****************************************
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <
http://mail.python.org/pipermail/chennaipy/attachments/20160701/c0c4700c/attachment-0001.html
>

------------------------------

Subject: Digest Footer

_______________________________________________
Chennaipy mailing list
Chennaipy@python.org
https://mail.python.org/mailman/listinfo/chennaipy


------------------------------

End of Chennaipy Digest, Vol 35, Issue 2
****************************************
_______________________________________________
Chennaipy mailing list
Chennaipy@python.org
https://mail.python.org/mailman/listinfo/chennaipy

Reply via email to