name space problem
Bruno Desthuilliers
bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Oct 24 08:27:14 EDT 2007
BBands a écrit :
> On Oct 23, 4:20 pm, marek.ro... at wp.pl wrote:
>> Hello. Indeed the doStuff function in the doStuff module can't do 'a.b
>> = 0' (the double dot was just a typo, right?)
>
> Yes.
>
>> because it doesn't know anything about an object named a.
>
> I was trying to understand why it worked when written in, but not when
> included.
because it's *not* "included" ? Python's imports are not includes, and
the notion of "global" namespace in Python really means module's namespace.
>> I think the right solution would be not to use 'a' as a global
>> variable, but rather to pass it as an explicit parameter to the
>> function.
>
> Does doing this make a copy of a?
No. Python nevers copy anything unless very explicitelly asked to. But
remember that names are locals to their namespaces, so that rebinding
'a' in doStuff won't affect the object originally bound to 'a' (mutating
the object bound to 'a' will of course work as expected).
More information about the Python-list
mailing list