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?

Increasingly Idiotic
  • 5,700
  • 5
  • 35
  • 73
Bella
  • 414
  • 2
  • 13
  • Take a look at getattr / setattr (https://docs.python.org/3/library/functions.html#setattr) – awesoon Apr 20 '18 at 06:35
  • You're looking for `getattr`, `getattr(dictionary['Obj1'], attribute)` – Taku Apr 20 '18 at 06:35
  • `.attribute` is not a variable there, so you can not replace that with the real variable `attribute` in your for loop. – BcK Apr 20 '18 at 06: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`? – cdarke Apr 20 '18 at 06:37
  • Possible duplicate of [A get() like method for checking for Python attributes](https://stackoverflow.com/questions/355539/a-get-like-method-for-checking-for-python-attributes) – CristiFati Apr 20 '18 at 06:41

3 Answers3

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)
CristiFati
  • 38,250
  • 9
  • 50
  • 87
0

Try something like this:

def calculate_something(dictionaryWithObjects):
    for attribute in ['attr1', 'attr2', 'attr3']:
        dataA1, dataA2 = dictionaryWithObjects['Object1'][attribute]
cdarke
  • 42,728
  • 8
  • 80
  • 84
NoorJafri
  • 1,787
  • 16
  • 27
  • 1
    this doen't work because the attribute is not a key.. It's an object. I've tried that in the beggining x) – Bella Apr 20 '18 at 06:39
  • 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. – NoorJafri Apr 20 '18 at 06:44
  • @NoorAliJafri you would generally want to use `vars` instead of `dir`, but why would you, if you can just us `getattr`? – juanpa.arrivillaga Apr 20 '18 at 07:06
  • @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 – NoorJafri Apr 20 '18 at 07:17
0

You can use eval:

data=eval('Object1.'+attribute)
Artemis
  • 2,553
  • 7
  • 21
  • 36
  • Doesn't work (in the question context), but even if it did, it would be one of the ugliest and avoidable ways. – CristiFati Apr 20 '18 at 08:19
  • Because your code snippet doesn't fit in the question context (where there's no "Obj"). – CristiFati Apr 20 '18 at 15:35
  • 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. – Artemis Apr 20 '18 at 19:19