Referencing a class variable
Question:
I have a class with several variables inside
class myClass():
def __init__(self):
self.first = ""
self.second = ""
self.third = ""
Ok, so if I then initialize a variable to that class, I understand that I can easily reference or set any of those class variables like this
myClassVar = myClass()
myClassVar.first = "foo"
myClassVar.second = "bar"
What I’m wanting to know is, what if the class variable I want to set is itself determined by another variable. for instance, if I looped through a list ["first", "second", "third"], is there any way to reference those class variables like this:
varList = ["first", "second", "third"]
for item in varList:
myClass.item = "whatever I want to set it to"
Something like what I just did will result in "myClass has no ‘item’ member"
Answers:
You can use setattr()
. It takes the attribute name as string:
class myClass:
def __init__(self):
self.first = ""
self.second = ""
self.third = ""
varList = ["first", "second", "third"]
myClassVar = myClass()
for item in varList:
setattr(myClassVar, item, "whatever I want to set it to")
print(myClassVar.first)
print(myClassVar.second)
output:
whatever I want to set it to
whatever I want to set it to
Something like what I just did will result in "myClass has no ‘item’
member"
That shouldn’t happen if you’re working with normal attributes. Python allows you to dynamically assign new attributes just like you did. But now myClassVar
has item
attribute in its namespace.
If you use slots for example, it prevents you from doing so:
class myClass:
__slots__ = ("first", "second", "third")
def __init__(self):
self.first = ""
self.second = ""
self.third = ""
varList = ["first", "second", "third"]
myClassVar = myClass()
for item in varList:
myClassVar.item = "whatever I want to set it to"
I have a class with several variables inside
class myClass():
def __init__(self):
self.first = ""
self.second = ""
self.third = ""
Ok, so if I then initialize a variable to that class, I understand that I can easily reference or set any of those class variables like this
myClassVar = myClass()
myClassVar.first = "foo"
myClassVar.second = "bar"
What I’m wanting to know is, what if the class variable I want to set is itself determined by another variable. for instance, if I looped through a list ["first", "second", "third"], is there any way to reference those class variables like this:
varList = ["first", "second", "third"]
for item in varList:
myClass.item = "whatever I want to set it to"
Something like what I just did will result in "myClass has no ‘item’ member"
You can use setattr()
. It takes the attribute name as string:
class myClass:
def __init__(self):
self.first = ""
self.second = ""
self.third = ""
varList = ["first", "second", "third"]
myClassVar = myClass()
for item in varList:
setattr(myClassVar, item, "whatever I want to set it to")
print(myClassVar.first)
print(myClassVar.second)
output:
whatever I want to set it to
whatever I want to set it to
Something like what I just did will result in "myClass has no ‘item’
member"
That shouldn’t happen if you’re working with normal attributes. Python allows you to dynamically assign new attributes just like you did. But now myClassVar
has item
attribute in its namespace.
If you use slots for example, it prevents you from doing so:
class myClass:
__slots__ = ("first", "second", "third")
def __init__(self):
self.first = ""
self.second = ""
self.third = ""
varList = ["first", "second", "third"]
myClassVar = myClass()
for item in varList:
myClassVar.item = "whatever I want to set it to"