0
for top in [eye_patch,hat]:
    test1 = pa.test(top_type=pa.TopType.top)

This is the code I wrote but I need it to basically say

test1 = pa.test(top_type=pa.TopType.eye_patch)

after which it runs

test1 = pa.test(top_type=pa.TopType.hat)

how do I get it to do this?

Kensac
  • 15
  • 1
  • 2
    Hard to understand exactly what your structure is as you have not provided a [minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (see also [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask)). I suggest you follow the guides there as it will help others answer your questions. That all said, [this post](https://stackoverflow.com/questions/11637293/iterate-over-object-attributes-in-python) might give you what you're looking for. – ramzeek Dec 12 '21 at 06:14
  • This really doesn't make a lot of sense. Please provide a [mcve] – juanpa.arrivillaga Dec 12 '21 at 07:02
  • Welcome to Stack Overflow. It is not really clear exactly what you're trying to do, why you're trying to do it, or whether you *have a good reason* to do it. In particular: it is not clear what the `eye_patch` and `hat` variables should contain, or how you expect them to be related to `pa.TopType.eye_patch` and `pa.TopType.hat`. If you wanted them to be strings, then they need to be quoted in your example code. It's also not clear what `pa.TopType` **is** (an enumeration? An ordinary class? A module?) The linked duplicate will work as asked regardless, but could be a very bad idea. – Karl Knechtel Dec 12 '21 at 07:58

2 Answers2

0

If you want to call an object's attribute by it's name, you need to use the getattr('name') function and pass your function name as a string.

Your code would look like this:

for top in [eye_patch,hat]:
    test1 = pa.test(top_type=getattr(pa.TopType, top.__name__)())

top.__name__ is for getting the function name as a string to call it as an attribute.

ext
  • 301
  • 1
  • 11
  • wait, how do we know `top` is a function? – juanpa.arrivillaga Dec 12 '21 at 06:58
  • @juanpa.arrivillaga sorry my bad, i misread the question. it would still work with an object – ext Dec 12 '21 at 06:59
  • huh? What do you mean "with an object"? *Everything* is an object in Python. And this will only work with objects that have a `__name__` attribute that happens to return the strings `"eye_patch"` and `"hat"` (which even in the case of functions is not something you can be sure about in this situation wihtout more details) – juanpa.arrivillaga Dec 12 '21 at 07:02
  • @juanpa.arrivillaga that was what i meant, it would work with anything you pass in that list. i can still just presume that `eye_patch` and `hat` have that attribute, no matter what type they are – ext Dec 12 '21 at 07:25
  • No, you absolutely *cannot presume that*, why do you believe you can? – juanpa.arrivillaga Dec 12 '21 at 07:27
  • @juanpa.arrivillaga because the question author did not specify that `eye_patch` and `hat` are not default objects or whatever else without default attributes – ext Dec 12 '21 at 07:29
  • **`__name__`** is not a default attribute. Most objects do not have a `__name__` attribute. – juanpa.arrivillaga Dec 12 '21 at 07:30
  • @juanpa.arrivillaga ok but for some reason the question author did not say anything bad about it. let us not continue arguing – ext Dec 12 '21 at 07:31
  • I'm not arguing. I'm pointing out your answer is wrong for people who read it. – juanpa.arrivillaga Dec 12 '21 at 07:32
  • I had thought it would be better to just edit the post rather than argue; but then I realized that we cannot really make assumptions about why the original code has `[eye_patch, top]` in the list rather than `['eye_patch', 'top']`. The values with those names could be anything, and OP could have any number of ideas about how the values should relate to the attributes of `TopType`. – Karl Knechtel Dec 12 '21 at 07:56
0

I think what you exactly want is:

from pa import test, TopType

for top in [TopType.eye_patch, TopType.hat]:
    test1 = test(top_type=top)

or an alternative:

from pa import test, TopType

for top in ['eye_patch', 'hat']:
    test1 = test(top_type=getattr(TopType, top))

but make sure that eye_patch and hat are both methods of the TopType.