a quick question about namespaces

Discussion in 'Python' started by Jay donnell, Feb 2, 2005.

  1. Jay  donnell

    Jay donnell Guest

    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?
    Jay donnell, Feb 2, 2005
    #1
    1. Advertising

  2. 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
    Steven Bethard, Feb 2, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Random

    Namespaces - broad question

    Random, Apr 8, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    516
    Kevin Spencer
    Apr 8, 2004
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    646
    Iain Norman
    Feb 16, 2005
  3. Omar
    Replies:
    4
    Views:
    18,076
    Patrick TJ McPhee
    Jun 21, 2005
  4. Bernd Fuhrmann
    Replies:
    8
    Views:
    368
    Rob Williscroft
    Dec 28, 2003
  5. JKop
    Replies:
    11
    Views:
    877
Loading...

Share This Page