Variables with cross-module usage

Nitin Changlani. changlani.nitin at gmail.com
Sat Nov 28 21:46:25 EST 2009


Thanks for the reply MRAB, Rami, Matt and Mel,

I was assuming that since one.myList0] = one.a, the change in one.a will
ultimately trickle down to myList[0] whenever myList[0] is printed or used
in an expression. It doesn't come intuitively to me as to why that should
not happen. Can you kindly suggest what is the correct way to go about it?

Nitin.

>
>
>
> > Hi Nitin,
> >
> > On Sat, Nov 28, 2009 at 14:36, MRAB <python at mrabarnett.plus.com> wrote:
> >> Nitin Changlani. wrote:
> >>> three.py
> >>> ------------
> >>> import one
> >>> import two
> >>>
> >>> def argFunc():
> >>> one.x = 'place_no_x'
> >>> one.a = 'place_no_a'
> >>> one.b = 'place_no_b'
> >>>
> >
> > I think this is what is biting you. You might expect that after
> > argFunc, one.x would be set to 'place_no_x' and so on. However,
> > Python's scoping doesn't work like that -- the name one.x is only
> > rebound in the function's scope, so outside of argFunc (e.g. in your
> > main printing code) one.x is still bound to 'place_x'.
>
> No, It's not like that.  MRAB had it.  The thing is, that when one.py is
> imported, it sets the name one.a to refer to a string 'place_a'.  Then a
> list named one.myList is built with one.myList[0] referring to the same
> string as one.a .  So far, so good.
>
> Then the function argFunc is called.  It uses `one` as a name from its
> global namespace.  Inside argFunc, the names one.x and one.a are rebound to
> different strings from the ones they started with.  *But* one.myList[0]
> isn't touched, and still refers to 'place_x' like it always did.
>
>        Mel.
>
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091128/e0abb29d/attachment-0001.html>


More information about the Python-list mailing list