Variables with cross-module usage

Rami Chowdhury rami.chowdhury at gmail.com
Sat Nov 28 18:06:11 EST 2009


--------
Rami Chowdhury
"Never assume malice when stupidity will suffice." -- Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)



On Sat, Nov 28, 2009 at 14:57, Matt Nordhoff <mnordhoff at mattnordhoff.com> wrote:
> Rami Chowdhury wrote:
>> 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'.
>>
>> HTH,
>> Rami
>
> Not true. argFunc does not rebind the name "one", it mutates the module
> object referred to by the name "one". Since there is only one instance
> of a given module*, the change is indeed reflected everywhere the "one"
> module is accessed.

Ah, thanks for clarifying!



More information about the Python-list mailing list