0

Here what I want to get:

class Foo:

    def __init__(self, test1, test2, test3):
     self.test1=test1
     self.test2=test2
     self.test3=test3

Is there a way to get list of member variable names?

Something similar like dir() function, but instead of this:

dir(Foo)
['__doc__', '__init__', '__module__']

You would have:

something(Foo)
['test1', 'test2', 'test3']
Gustav Bertram
  • 14,591
  • 3
  • 40
  • 65
Vy.Iv
  • 829
  • 2
  • 8
  • 17
  • possible duplicate of [Printing all instances of a class](http://stackoverflow.com/questions/328851/printing-all-instances-of-a-class) – Dan Nov 29 '13 at 03:09
  • 2
    Title (how to get all instances) and question body (how to list object members) disagree in meaning. Make sure to use the correct diction - and that the title and body of the post agree. – user2864740 Nov 29 '13 at 03:10

2 Answers2

5

You're defining instance variables, not class variables. To get instance variables, you'll have to instantiate it:

>>> f = Foo(1, 2, 3)
>>> dir(f)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'test1', 'test2', 'test3']

There, you have all the attributes.

But if you only want the attributes you declared, use f.__dict__:

>>> f.__dict__
{'test3': 3, 'test2': 2, 'test1': 1}

Or alternatively, use vars(f).

But if you wanted to get the class variables, just refer to the class itself:

>>> class Foo:
    abcd = 10
    def __init__(self, test1, test2, test3):
       self.test1=test1
       self.test2=test2
       self.test3=test3

>>> vars(Foo)
mappingproxy({'abcd': 10, '__dict__': <attribute '__dict__' of 'Foo' objects>, '__doc__': None, '__module__': '__main__', '__init__': <function Foo.__init__ at 0x00000000032290D0>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>})
>>> dir(Foo)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'abcd']

Hope this helps!

aIKid
  • 26,968
  • 4
  • 39
  • 65
  • Thanks. I see your point, but is there a way to get a instance variables without instantiating it? Lets say we import class from model. Just pass class, check what instance variables it has. – Vy.Iv Nov 30 '13 at 01:30
  • Unless you declared them as class variables, you can't. That `self` refer to "an instance of this class", so when you said `self.test1`, you can only get them from an instance :) – aIKid Nov 30 '13 at 06:36
1

General way to stores instances is using a class variable. But I'm not sure if this is what you want:

class Foo:
    names = {}

    def __init__(self, name):
        self.name = name
        Foo.names[name] = self

f1, f2, f3 = Foo('name1'), Foo('name2'), Foo('name3')
print Foo.names
perreal
  • 94,503
  • 21
  • 155
  • 181
  • Beware, if you declare this class at the top level of the module, instances will never get garbage collected unless you delete the reference from `Foo.names`, even if you `del(f1)`. – Paulo Scardine Nov 29 '13 at 03:48