I am probably approaching this wrong, but would appreciate being straightened out.
I would like to be able to use both the values and the names of some attributes of a class
Sample:
class DoStuff(object):
def __init__(self):
self.a="Alpha"
self.b="Beta"
self.c="Gamma"
def printStuff(self):
for thing in [self.a, self.b, self.c]:
print NAMEOFTHING, thing
What I want is:
a Alpha b Beta c Gamma
How can I get that?
Edit: Some confusion because my example showed me printing ALL the values. Instead I want this:
a Alpha c Gamma
with the list for my print method just having ‘a’ and ‘c’ in it.
The closest you could get is:
Variables can have multiple names and aren’t aware of their own name, so if you know it’s ‘a’, then you can use
getattrto resolve the lookup.Another option (although not greatly different than above)