1

I have a class

class MyClass():
    def __init__(self):
        self.a = 7
        self.b = 2
    @property
    def aAndB(self):
        return self.a + self.b

I would like a function that iterates over all properties and returns only class instances having a certain property.

My goal is a function like this:

def findInstances(listOfInstances, instanceVariable, instanceValue):
    #return all instances in listOfInstances where instanceVariable==instanceValue

Using instance.__dict__ only gives me a and b, but not aAndB. I would like to have a dict of all properties/methods with their values to loop over, so I can search for instances where a certain property (or method decorated with @property) has a certain value.

Currently, calling the function like this

findInstances(someListOfInstances, 'aAndB', '23')

makes Python complain that aAndB is not in instance.__dict__.


Maybe all of you are right and the answers are there, but I still don't get it. All the answers in the mentioned questions get lists, not dictionaries. I want all the properties (including methods with the @property decorator) and their values. Is there a way to iterate over the values of the keys in dir(myClass)? The dir command only contains the names of the attributes, not their values.

I need something like

for a in dir(myClass):
    print a, myClass.(a)  # get the value for an attribute stored in a variable

To be even more clear: The following achieves exactly what I want but there is probably a better way that I don't know.

for a in dir(myClass):
    print a, eval("myClass.{}".format(a))
9
  • You are not the first one who asks for this. Search and you will find plenty of answers which might help you. Commented Mar 3, 2017 at 14:25
  • 2
    Possible duplicate of Iterate over object attributes in python Commented Mar 3, 2017 at 14:26
  • Possible duplicate of How to enumerate an object's properties in Python? Commented Mar 3, 2017 at 14:28
  • @River found this links before, but they want to iterate over variables only, not methods as well. Commented Mar 3, 2017 at 14:33
  • @Stefan You can just filter them out using callable as shown in the answer... Read further down the top answer a bit Commented Mar 3, 2017 at 14:38

1 Answer 1

3

There's actually a very simple way to do this, using getattr in place of eval:

myClass = MyClass()
for a in dir(myClass):
    if(a[:2] != "__"): #don't print double underscore methods
        print a, getattr(myClass, a)

Output:

a 7
aAndB 9
b 2

This has the very big advantage of not needing to hard code in the name of your instance into the string, as is required using eval("myClass.{}".format(a))

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

Comments

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.