Namespace Problem with global declaration in module

spex66 at my-deja.com spex66 at my-deja.com
Thu Apr 27 05:25:42 EDT 2000


hmm, deja is not so fast as my newsreader :), so here is a reply of
Gordons message

>spex66 at my-deja.com wrote:
>
>> Hi,
>> it seemed very simple but don't worked like expected:
>>
>> #FILE: test.py
>> dd = None #INIT a shadow instance for automated access
>> class zz:
>>     #ONLY one class will be instantiated
>>     def __init__(self):
>>         self.name = 'hallo'
>>         global dd
>>         dd = self
>> #END OF FILE
>>
>> >>> from test import *
>
>There's your problem. "from test import *" copies names from
>test to your namespace. Now dd is another name for None.
>yy() affects the dd in test's namespace, not yours.
>
>- Gordon

I see the point, but I like the from statement :)
Is it a bug? It's not the way I expected this code to work... and I
cannot find a hook in the documentation that explicit explained this
misbehaviour.

Any idea for a workaround (I need the from-statement for further code
readability)?

thanks
Peter
(=PA=)

>
>> >>> yy = zz()
>> >>> dd.name
>>     ...
>>     AttributeError
>>     ...
>



Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list