简体   繁体   English

我可以使用具有 class 属性/function 的变量吗?

[英]Can I use a variable with a class attribute / function?

It doesn't allow me to use a variable with an attribute of the class.它不允许我使用具有 class 属性的变量。 Not sure how to manage this.不知道如何管理这个。 I need the answer to come out as 7. But I need to be able to use the variable for it.我需要得到 7 的答案。但我需要能够使用该变量。 gives me the error,给我错误,

Traceback (most recent call last): File "c:/Data/First Game (Python)/rough.py", line 32, in a.add_2(5) AttributeError: 'str' object has no attribute 'x'回溯(最后一次调用):文件“c:/Data/First Game (Python)/rough.py”,第 32 行,在 a.add_2(5) 中 AttributeError: 'str' object 没有属性 'x'

I think it is not able to recognize that by a I meant to mention Me1.我认为它无法通过我的意思提到 Me1 来识别这一点。

class Test():
    def add_2(self, y):
        print(y + 2)

Me1 = Test()

for i in range (1):
    a = "Me" + str(i+1)
    print (a)
    a.add_2(5)

From what I see the variable a is a string while the function a.add_2(5) belongs to the objects of class Test .从我看到的变量a是一个字符串,而 function a.add_2(5)属于 class Test的对象。 The error is caused by the fact that you call a method of class Test on a string该错误是由于您在字符串上调用 class Test方法引起的

It doesn't make any sense on constructing an object of a class at runtime unless you're exploring something to learn.除非您正在探索要学习的东西,否则在运行时构造 class 的 object 没有任何意义。 But, here is what you're looking for,但是,这就是你要找的,

class Test():
    def add_2(self, y):
        print(y + 2)

Me1 = Test()

for i in range (1):
    a = "Me" + str(i+1)
    print(a)
    b = eval(a) # converting str to Class Test object
    b.add_2(5)

Output: Output:

Me1
7

Reference: https://docs.python.org/3/library/functions.html#eval参考: https://docs.python.org/3/library/functions.html#eval

暂无
暂无

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

相关问题 如何在基类中使用子类的函数属性? - How can I use a child class' function attribute in the base class? 我可以使用另一个 class 内部的 function 中的变量吗? - Can I use a variable from a function that's inside of another class? 我可以将函数作为类属性传递并调用它吗? - Can I pass a function as a class attribute and call it? 在 Django 中,我可以使用传递给函数的变量来访问模型的属性吗? - In Django, can I use a variable passed to a function in order to access and attribute of a model? 如何在同一 class AttributeError: 'Hangman' object has no attribute 'word_store' 的另一个 function 中使用属性 word_store - How can I use attribute word_store in another function of the same class AttributeError: 'Hangman' object has no attribute 'word_store' 在python类中,如何在另一个函数中使用一个函数定义的变量? - In python class, how can I use the variable defined by one function in another function? 如何从 Python 中的变量定义类属性? - How can I define a class attribute from a variable in Python? 在 Python 中,如何将函数属性用作函数? - In Python, how can I use a function attribute as a function? 如何使用Python Class属性作为@Classmethod的默认值? - How can I use a Python Class attribute as a default value for a @Classmethod? 我可以使用class属性作为实例方法的默认值吗? - Can I use a class attribute as a default value for an instance method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM