0

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"

S.B
  • 13,077
  • 10
  • 22
  • 49
milnuts
  • 407
  • 2
  • 7
  • 16
  • Isn't the effect the same as using a dictionary where varlist defines the keys? – itprorh66 Apr 06 '23 at 19:57
  • 1
    You rarely initialize attributes like this. Define `__init__` to take values as arguments to set the attributes for you, and call it as `obj = MyClass("foo", 'bar", "baz)`. – chepner Apr 06 '23 at 20:04
  • 2
    Very important terminology note: you aren't talking about *class variables*, you are talking about *instance variables* – juanpa.arrivillaga Apr 06 '23 at 20:05
  • with `myClass.item` you're assigning something to the class itself. Apparently you wanted to instantiate the class first, then assign something to the instance. `myClassVar = myClass()` – S.B Apr 06 '23 at 20:07
  • @chepner agreed, I don't initialize the values the way I did in my example. I actually initialize them as you describe. I tried to simplify my example, because what I'm actually doing is trying to set and get those values later on based on other processed information – milnuts Apr 06 '23 at 21:12

1 Answers1

1

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"
S.B
  • 13,077
  • 10
  • 22
  • 49
  • 1
    Thanks, this was exactly what I was looking for. Confirmed that the getattr and setattr are doing what I want – milnuts Apr 06 '23 at 21:13