Namespace Problem with global declaration in module

Gordon McMillan gmcm at hypernet.com
Wed Apr 26 21:48:25 CEST 2000


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.

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


- Gordon




More information about the Python-list mailing list