0

I am working with objects in Python and all I wanted was to replace the attribute in a for loop to get the data out of the object. Here is my code:

def calculate_something(dictionaryWithObjects):
    for attribute in ['attr1', 'attr2', 'attr3']:
        dataA1, dataA2 = dictionaryWithObjects['Object1'].attribute
        dataB1, dataB2 = dictionaryWithObjects['Object2'].attribute
        dataC1, dataC2 = dictionaryWithObjects['Object3'].attribute

So this is more or less what I think I need. I will use the data for calculations afterwards. But it gives an error saying that the object has no attribute called ".attribute". Of course it doesn't, I meant it to replace the 'attr1' in there.

What am I doing wrong?

5
  • Take a look at getattr / setattr (docs.python.org/3/library/functions.html#setattr) Commented Apr 20, 2018 at 6:35
  • You're looking for getattr, getattr(dictionary['Obj1'], attribute) Commented Apr 20, 2018 at 6:35
  • .attribute is not a variable there, so you can not replace that with the real variable attribute in your for loop. Commented Apr 20, 2018 at 6:35
  • Since you are learning, once you have the getattr working, what happens if you remove the [ ] from the series of attribute names in the for? Commented Apr 20, 2018 at 6:37
  • Possible duplicate of A get() like method for checking for Python attributes Commented Apr 20, 2018 at 6:41

3 Answers 3

4

In order to access an instance attribute by its name, you should use [Python]: getattr(object, name[, default]), e.g.:

dataA1, dataA2 = getattr(dictionaryWithObjects['Object1'], attribute)
Sign up to request clarification or add additional context in comments.

3 Comments

does the dictionaryWithObjects.get("Object1",attribute) have same as u answered
@Roushan: True, but that wasn't part of the question, and I didn't want to overcomplicate the answer.
@Roushan: Only now I understood what you mean by your 1st comment. I thought it was replacing the [] by the get method.
0

Try something like this:

def calculate_something(dictionaryWithObjects):
    for attribute in ['attr1', 'attr2', 'attr3']:
        dataA1, dataA2 = dictionaryWithObjects['Object1'][attribute]

4 Comments

this doen't work because the attribute is not a key.. It's an object. I've tried that in the beggining x)
Well can you paste the result for dir(dictionaryWithObjects) and dir(dictionaryWithObjects["Object1"]. This actually helps to get you with your loops over your whatever.
@NoorAliJafri you would generally want to use vars instead of dir, but why would you, if you can just us getattr?
@juanpa.arrivillaga I was letting her know to use python's cli with dir(yourobject) to have a better picture of the variable. That's it :D
0

You can use eval:

data=eval('Object1.'+attribute)

3 Comments

Doesn't work (in the question context), but even if it did, it would be one of the ugliest and avoidable ways.
Because your code snippet doesn't fit in the question context (where there's no "Obj").
I've changed it, however the point of SO questions is to also solve problems for future users, so it makes no sense to be entirely context specific.

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.