引用类变量

huangapple go评论76阅读模式
英文:

Referencing a class variable

问题

我有一个包含多个变量的类

```python
class myClass():
   def __init__(self):
      self.first = ""
      self.second = ""
      self.third = ""

好的,所以如果我然后将一个变量初始化为该类,我了解到我可以轻松引用或设置这些类变量中的任何一个,就像这样

myClassVar = myClass()

myClassVar.first = "foo"
myClassVar.second = "bar"

我想知道的是,如果我想设置的类变量本身是由另一个变量确定的,例如,如果我遍历一个列表 ["first", "second", "third"],是否有办法像这样引用这些类变量:

varList = ["first", "second", "third"]
for item in varList:
   myClass.item = "我想设置的任何内容"

像我刚才做的那样会导致"myClass没有'item'成员"。

英文:

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"

答案1

得分: 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"
英文:

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"

huangapple
  • 本文由 发表于 2023年4月7日 03:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定