【问题标题】:How to convert a string to an attribute when the string has the same value with the attribute当字符串与属性具有相同值时如何将字符串转换为属性
【发布时间】:2021-12-30 23:10:13
【问题描述】:
class Person:  
  __name = ""
  __contacts = [] 
  __age = 0
  __vaccination_state = False
  __health_state = True 
  __quarantine_state = True 
  __symptom = "symptomatic" 

  def __init__(self, name, contacts, age, vaccination_state, health_state, quarantine_state, symptom):
    self.__name = name
    self.__age = age 
    self.__contacts = contacts 
    self.__vaccination_state = vaccination_state
    self.__health_state = health_state
    self.__quarantine_state = quarantine_state
    self.__symptom = symptom 

  def GetName(self):
    return self.__name

  def GetAge(self):
    return self.__age
  
  def GetContacts(self):
    return self.__contacts

  def GetVaccination(self):
    return self.__vaccination_state

  def GetHealth(self):
    return self.__health_state

  def GetQuarantine(self):
    return self.__quarantine_state

  def GetSymptom(self):
    return self.__symptom
 
  def SetName(self, name):
    self.__name = name

  def SetAge(self, age):
    self.__age = age

  def SetAllContacts(self, contacts):
    self.__contacts = contacts 

  def AddSingleContact(self, contact):
    self.__contacts.append(contact)

  def SetVaccination(self, vaccination_state):
    self.__vaccination_state = vaccination_state

  def SetHealth(self, healthState):
    self.__health_state = healthState
    self.CheckPositive()

  def SetQuarantine(self, quarantine_state):
    self.__quarantine_state = quarantine_state
  
  def SetSymptom(self, symptom):
    self.__symptom = symptom 
  def CheckPositive(self):
    if self.__health_state == False: 
      self.__quarantine_state = True
      start = datetime.datetime.now()
      end = start + datetime.timedelta(10)
      for i in range(len(self.__contacts)):
        getattr(Person, self.GetContacts()[i]).SetQuarantine(True)
        print(self.GetContacts()[i], " and his/her close contacts have been notified.\n", "Quarantine starts at", start, "and ends at", end)
        for i in range(len(self.__contacts)):
          getattr(Person, self.GetContacts()[i]).SetQuarantine(True)

A = Person("A", ["Z"], 17, True, True, False, "NA")
Z = Person("Z", ["A"], 17, True, True, False, "NA")
A.SetHealth(False) 

SetQuarantine(True) 是我要调用的类中的一个函数。 self.GetContacts(i) 的返回值是一个列表。所以 self.GetContacts()[i] 是一个字符串,例如“name”。但我想获得一个属性,以便我可以调用函数 SetQuarantine(True)。此属性与字符串具有相同的值,但我不知道如何转换它。 谢谢!

【问题讨论】:

  • 你能给我们Person类的代码吗?就目前的情况而言,您没有给我们提供最低限度可重现的示例,这使我们更难诊断问题。
  • self.__contacts 包含什么?与self.GetContacts 返回的列表相同吗?还提供minimal reproducible example
  • 感谢您的 cmets!我刚刚编辑了代码,我将 A 的健康状态设置为 false,按照逻辑,Z 的隔离状态应该设置为 True。但由于它是一个字符串,它不能调用函数。你能帮我调用可以触发函数 SetQuarantine() 的函数吗?谢谢@BrokenBenchmark
  • @Matiiss 感谢您的评论。我在 BrokenBenchmark 的回复中回答了您的问题。

标签: python class


【解决方案1】:

您的问题源于尝试执行此操作:getattr(Person, self.GetContacts()[i]).SetQuarantine(True)。您正在尝试访问不存在的 Person.Z 属性。 Python 会因此引发错误。

你需要做两件事来解决这个问题:

  1. 不要(尝试)使用类级变量。当您希望在类的所有实例之间共享数据时,应使用类级变量。由于 Person 的不同实例可以有不同的联系人,因此在这里使用类级别的变量没有什么意义。
  2. 在每个 Person 实例中,您需要为每个联系人存储对 Person 对象的引用,而不是他们的姓名。这使您可以直接设置密切接触者的隔离状态。 (即使您要创建一个姓名查找表,该表也无法处理同名的两个人。)

这是修改后的CheckPositive 函数以及一些修改后的驱动程序代码:

def CheckPositive(self):
  if self.__health_state == False: 
    self.__quarantine_state = True
    start = datetime.datetime.now()
    end = start + datetime.timedelta(10)
    for i in range(len(self.__contacts)):
      print(self.__contacts[i].GetName(), "has been notified.\n", "Quarantine starts at", start, "and ends at", end)
      self.__contacts[i].SetQuarantine(True)

A = Person("A", [], 17, True, True, False, "NA")
Z = Person("Z", [], 17, True, True, False, "NA")
A.AddSingleContact(Z)
Z.AddSingleContact(A)
A.SetHealth(False)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2023-03-06
    • 2019-02-25
    • 1970-01-01
    • 2016-11-23
    相关资源
    最近更新 更多