a quick question about namespaces

J

Jay donnell

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

Google mungs up the spacing so I put a - in place of spaces. Does
anyone know how to get around this spacing issue on google groups?
 
S

Steven Bethard

Jay said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top