[BangPypers] Enclosing lexical context

2010-04-15 Thread Picachu Nioto
Could some one explain to me this sentence, I read in an example online

Python doesn't implement assignment of variables bound in an enclosing
lexical context

Example,
a=[b]

--Picachu
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Enclosing lexical context

2010-04-15 Thread Noufal Ibrahim
On Thu, Apr 15, 2010 at 10:36 PM, Picachu Nioto picachu.ni...@gmail.com wrote:
 Could some one explain to me this sentence, I read in an example online

 Python doesn't implement assignment of variables bound in an enclosing
 lexical context

 Example,
 a=[b]

I'm not sure where you got this sentence means but Python's scoping is
lexical but has 2 namespaces accessible from the current point of
execution (locals and globals) which are arguably dynamic. Here's an
example to show lexical scoping.

http://pastebin.com/k2S5pvjZ

You can see that it prints 1 which is the value of the free identifier
foo lexically at the point of the print.

As a counter example, here's an example in Emacs lisp which parallels
the above but since it's dynamically scoped, the value printed is 2.
http://pastebin.com/KX0JwC0u



-- 
~noufal
http://nibrahim.net.in
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Enclosing lexical context

2010-04-15 Thread Picachu Nioto
Thanks for the prompt response Noufal!

Could you explain, in what context is it mentioned here?
http://pastebin.com/zLnL7yy8

http://pastebin.com/zLnL7yy8

On Thu, Apr 15, 2010 at 11:09 PM, Noufal Ibrahim nou...@gmail.com wrote:

 On Thu, Apr 15, 2010 at 10:36 PM, Picachu Nioto picachu.ni...@gmail.com
 wrote:
  Could some one explain to me this sentence, I read in an example online
 
  Python doesn't implement assignment of variables bound in an enclosing
  lexical context
 
  Example,
  a=[b]

 I'm not sure where you got this sentence means but Python's scoping is
 lexical but has 2 namespaces accessible from the current point of
 execution (locals and globals) which are arguably dynamic. Here's an
 example to show lexical scoping.

 http://pastebin.com/k2S5pvjZ

 You can see that it prints 1 which is the value of the free identifier
 foo lexically at the point of the print.

 As a counter example, here's an example in Emacs lisp which parallels
 the above but since it's dynamically scoped, the value printed is 2.
 http://pastebin.com/KX0JwC0u



 --
 ~noufal
 http://nibrahim.net.in
 ___
 BangPypers mailing list
 BangPypers@python.org
 http://mail.python.org/mailman/listinfo/bangpypers

___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Enclosing lexical context

2010-04-15 Thread Anand Balachandran Pillai
On Thu, Apr 15, 2010 at 10:36 PM, Picachu Nioto picachu.ni...@gmail.comwrote:

 Could some one explain to me this sentence, I read in an example online

 Python doesn't implement assignment of variables bound in an enclosing
 lexical context

 a=[10]
 def f(x):
... a[0]=x
... print a
...
 f(2)
[2]
 print a
[2]

In this case, the outer a is accessed automatically since we are using
indices and there is no  local list a, Python finds the scope from
the global scope and assigns correctly.


 Example,
 a=[b]


Looking at your code above, perhaps the 2nd explanation
makes it clear.


 --Picachu
 ___
 BangPypers mailing list
 BangPypers@python.org
 http://mail.python.org/mailman/listinfo/bangpypers




-- 
--Anand
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Enclosing lexical context

2010-04-15 Thread Anand Balachandran Pillai
My reply got deleted partly before I sent it. Here is the full one.

It is not clear from your email what context means but mostly it
is the fact that Python differentiates between local and global
scope when variables are assigned.

For example,

 a=10
 def f(x):
... a=x
... print a
...
 f(20)
20
 print a
10

The global a is still unmodified since the f() function changes only
its local a. To fix this we need to prefix the local a with global.

 def f(x):
... global a
... a=x
... print a
...
 f(20)
20
 print a
20

However, this can also be done using a container as below.

 a=[10]
 def f(x):
... a[0]=x
... print a
...
 f(2)
[2]
 print a
[2]

In this case, the outer a is accessed automatically since we are using
indices and there is no  local list a, Python finds the scope from
the global scope and assigns correctly.

Looking at your code above, perhaps the 2nd explanation
makes it clearer and seems closer to what you are expecting
as answer.

--Anand



On Thu, Apr 15, 2010 at 11:19 PM, Anand Balachandran Pillai 
abpil...@gmail.com wrote:



 On Thu, Apr 15, 2010 at 10:36 PM, Picachu Nioto 
 picachu.ni...@gmail.comwrote:

 Could some one explain to me this sentence, I read in an example online

 Python doesn't implement assignment of variables bound in an enclosing
 lexical context

  a=[10]
  def f(x):
 ... a[0]=x
 ... print a
 ...
  f(2)
 [2]
  print a
 [2]

 In this case, the outer a is accessed automatically since we are using
 indices and there is no  local list a, Python finds the scope from
 the global scope and assigns correctly.


 Example,
 a=[b]


 Looking at your code above, perhaps the 2nd explanation
 makes it clear.


 --Picachu
 ___
 BangPypers mailing list
 BangPypers@python.org
 http://mail.python.org/mailman/listinfo/bangpypers




 --
 --Anand






-- 
--Anand
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Enclosing lexical context

2010-04-15 Thread Anand Chitipothu
2010/4/15 Picachu Nioto picachu.ni...@gmail.com:
 Could some one explain to me this sentence, I read in an example online

 Python doesn't implement assignment of variables bound in an enclosing
 lexical context

 Example,
 a=[b]

Consider the following example:

a = 1
def f():
   b = 2
def g():
c = 3

# this function can access all a, b and c variables.
print a, b, c

# c can be reassigned
c = 42

# a can be reassigned only if it is declared as global,
otherwise it is considered as local to this function
global a
a = 42

   # b can't be reassigned because it is neither local nor global.
It is in the enclosing lexical context

SInce b can't be reassigned, the work-around is to modify the object
instead of reassigning.

However, python3.0 added a new nonlocal construct to enable that.
With python 3, you should be able to say:

nonlocal b
b = 42

Anand
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Enclosing lexical context

2010-04-15 Thread Roshan Mathews
On Thu, Apr 15, 2010 at 23:33, Anand Chitipothu anandol...@gmail.com wrote:
 However, python3.0 added a new nonlocal construct to enable that.
 With python 3, you should be able to say:

So there are globals, locals, and nonlocals.

:)
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers