a quick question about namespaces

Steven Bethard steven.bethard at gmail.com
Wed Feb 2 01:53:41 EST 2005


Jay donnell wrote:
> in the code below 'print locals()' shows mc2. What is the equivalent
> way to see the namespace that mc resides in?
> 
> 
> class myClass:
> --def func1(self):
> ----self.mc = 1
> ----mc2 = 3
> ----print 'in myClass.func1'
> ----print 'printing locals'
> ----print locals()
> ----print

I think you're looking for vars(self) or self.__dict__:

py> class MyClass(object):
...     def func1(self):
...         self.mc = 1
...         mc2 = 3
...         print locals()
...         print vars(self)
...         print self.__dict__
...
py> MyClass().func1()
{'self': <__main__.MyClass object at 0x027D8550>, 'mc2': 3}
{'mc': 1}
{'mc': 1}

HTH,

Steve



More information about the Python-list mailing list