简体   繁体   English

从输入 python 调用 object 调用 function

[英]Call function referring to object from input python

So I have a class with a print function.所以我有一个 class,印有 function。

Goal: I want to be able to write "c1" in the input field to make a call equivalent to c1.prnt().目标:我希望能够在输入字段中写入“c1”以进行等同于 c1.prnt() 的调用。

Current error says " AttributeError: 'str' object has no attribute 'prnt' "当前错误显示“ AttributeError: 'str' object has no attribute 'prnt'

I guess the input is passed as a string but should be passed as a pointer to c1 in this case, how do I do this?我想输入是作为字符串传递的,但在这种情况下应该作为指向 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()

Put the objects in a dictionary so you can map names to the objects.将对象放入字典中,这样您就可以为对象命名 map。

claims = {}
claims['c1'] = claim("Tax is mean",[s1,s2])

claims[input("What claim do you want to print?")].prnt()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM