从输入 python 引用对象的调用函数
Call function referring to object from input python
所以我有一个 class 具有打印功能。
目标: 我希望能够在输入栏中写上 "c1" 来制作
相当于 c1.prnt().
的调用
当前错误显示“AttributeError: 'str' 对象没有属性 'prnt'”
我想输入是作为字符串传递的,但在这种情况下应该作为指向 c1 的指针传递,
我该怎么做?
class claim:
def __init__(self,text,support_list):
authors = ["Author A","Author B"]
#base_properties.__init__(self,authors)
self.text = text
self.support_list = support_list
def prnt(self):
n = len(self.support_list)-1
print(f"\nThe claim -{self.text}- is backed by {c1.support_list[n].name} and {n} more.")
c1 = claim("Tax is mean",[s1,s2])
input("What claim do you want to print?").prnt()
将对象放入字典中,以便您可以将名称映射到对象。
claims = {}
claims['c1'] = claim("Tax is mean",[s1,s2])
claims[input("What claim do you want to print?")].prnt()
所以我有一个 class 具有打印功能。
目标: 我希望能够在输入栏中写上 "c1" 来制作 相当于 c1.prnt().
的调用当前错误显示“AttributeError: 'str' 对象没有属性 'prnt'”
我想输入是作为字符串传递的,但在这种情况下应该作为指向 c1 的指针传递, 我该怎么做?
class claim:
def __init__(self,text,support_list):
authors = ["Author A","Author B"]
#base_properties.__init__(self,authors)
self.text = text
self.support_list = support_list
def prnt(self):
n = len(self.support_list)-1
print(f"\nThe claim -{self.text}- is backed by {c1.support_list[n].name} and {n} more.")
c1 = claim("Tax is mean",[s1,s2])
input("What claim do you want to print?").prnt()
将对象放入字典中,以便您可以将名称映射到对象。
claims = {}
claims['c1'] = claim("Tax is mean",[s1,s2])
claims[input("What claim do you want to print?")].prnt()