Implementation of the global statement

Mikael Olofsson mikael at isy.liu.se
Wed Nov 27 10:00:22 EST 2002


Hi all!

On 27 Nov 2002 05:26:33 -0800
hungjunglu at yahoo.com (Hung Jung Lu) wrote:
> The global statement as in
> 
> def f(x):
>     global y
>     ...
> 
> is a directive to the parser. Internally the parser must keep a list
> of the "locally-globalled" names. Yes, you guessed right, out of pure
> curiosity: is this list accessible from Python? If not, why not? :)

I have a few related questions. Assume that we have defined f as above
in one global namespace, A say, and assume that f (re)binds y. Then we
place f in another global namespace, B say. What happens when we execute
f in B is that y in A is rebound. I would have guessed that a variable y
in B would have been created or rebound. Now to my questions:

  Why is that so? 
  Is that behaviour intended?
  Is it likely to stay that way?

Please understand me correcly. I'm not complaining. I'm just curious.
Actually, in my application, the described behaviour is what I'm after.
I could also imagine that there are situations where someone would
prefer y in B to be affected, and not y in A, which brings me to my last
question:

  Is there an obvous way to have f (still defined in A) manipulate
  objects in B when executed in B?

Greetings
/Mikael

-----------------------------------------------------------------------
E-Mail:  mikael at isy.liu.se
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael               
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by Sylpheed.
-----------------------------------------------------------------------
Linköpings kammarkör: www.kammarkoren.com




More information about the Python-list mailing list