1

From

a = []

class A(object):
    def __init__(self):
        self.myinstatt1 = 'one'
        self.myinstatt2 = 'two'

to

a =['one','two']
1

2 Answers 2

9

Python has a handy builtin called vars that will give the attributes to you as a dict:

>>> a = A()
>>> vars(a)
{'myinstatt2': 'two', 'myinstatt1': 'one'}

To get just the attribute values, use the appropriate dict method:

>>> vars(a).values()
['two', 'one']

In python 3, this will give you a slightly different thing to a list - but you can just use list(vars(a).values()) there.

Sign up to request clarification or add additional context in comments.

1 Comment

didn't know about vars, nice!
2

Try to look into the __dict__ attribute. It will help you:

a = A().__dict__.values()
print a
>>> ['one', 'two']

1 Comment

Given that A is the class, shouldn't it be A().__dict__.values()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.