[英]How can I use the value of a variable in a method inside another python file?
I have the variable name
inside the method control_in(self)
which is inside the class Test
. 我在类Test
里面的control_in(self)
方法中有变量name
。
I have tried to make name
an attribute by doing: 我尝试通过以下方式使name
成为属性:
a) setattr(control_in, name
,"Harry Banes") b) control_in.name = "Harry Banes" a)setattr(control_in, name
,“ Harry Banes”)b)control_in.name =“ Harry Banes”
In file b.py I have tried using: a) from a import *
b) import a
c) from a import Test
aa) print(a.Test.control_in.name)
ab) print(t1.control_in.name)
ac) print(a.t1.control_in.name)
ad) global name
in every part of the code (I know this is useless but just wanted to mention I tried it.) 在文件b.py我已经尝试使用:1) from a import *
B) import a
C) from a import Test
AA) print(a.Test.control_in.name)
AB) print(t1.control_in.name)
AC)代码的每个部分中的print(a.t1.control_in.name)
广告) global name
(我知道这是没有用的,但只是想提一下我尝试过。)
I have also tried return name
and return control_in.name
at the end of the control_in(self) method 我也尝试过在control_in(self)方法的末尾return name
和return control_in.name
and none print the value of the name variable. 并且没有显示名称变量的值。 But everything I put in init method, it does print. 但是我放入init方法的所有内容都可以打印。 Take print(t1.n)
for example that prints: numbers
. 以print(t1.n)
为例,它打印: numbers
。
YES, I've already searched and tried a lot of the solutions other StackOverflow Q&A had and none has worked which is why I am asking personally. 是的,我已经搜索并尝试了许多其他StackOverflow问题与解答所没有的解决方案,这就是为什么我要亲自问一下。
a.py a.py
import b
class Test:
def __init__(self):
self.n = "numbers"
def control_in(self):
control_in.name = "Harry Banes"
if __name__ == '__main__':
pass
b.py b.py
from a import *
if __name__ == '__main__':
t1 = Test()
print(t1.n) # Successfuly prints "numbers"
print(t1.control_in.name) # Throws attribute error
I expect the output "Harry Banes" but instead I get this error: AttributeError: 'function' object has no attribute 'name'
我期望输出“ Harry Banes”,但是却收到此错误: AttributeError: 'function' object has no attribute 'name'
I want to use the value from another Class' methods other than init , in another file. 我想在另一个文件中使用init以外的其他Class方法的值。 Why I want this? 为什么我要这个? Because It's a problem I am having for a way bigger code where I need just 1 variable to make something happen in another function, from another python file but I don't know how to get the variable value. 因为这是一个问题,所以我需要一种更大的代码,其中我只需要一个变量即可使另一个Python文件中的另一个函数中发生某些事情,但我不知道如何获取变量值。
Firstly, control_in.name = "Harry Banes"
is not a valid code because a class method cannot be referred to just by its name, but by its class: 首先, control_in.name = "Harry Banes"
不是有效的代码,因为不能仅通过其名称而是通过其类来引用类方法:
def control_in(self):
Test.control_in.name = "Harry Banes"
or better yet, use self.__class__
to refer to the class: 或self.__class__
是,使用self.__class__
来引用该类:
def control_in(self):
self.__class__.control_in.name = "Harry Banes"
Secondly, Test.control_in.name
won't be defined until the control_in
method is called and executed, so you have to specifically call it: 其次,在调用并执行control_in
方法之前,不会定义Test.control_in.name
,因此您必须专门调用它:
t1 = Test()
t1.control_in()
print(t1.control_in.name)
This outputs: 输出:
Harry Banes
But lastly, there usually isn't any good reason to add attributes to a function object. 但是最后,通常没有充分的理由向功能对象添加属性。 In this case the Test.control_in.name
variable is bound to a class method and is therefore shared across all instances, and should more idiomatically be redefined as a class variable instead: 在这种情况下, Test.control_in.name
变量绑定到类方法,因此在所有实例之间共享,因此应更惯用地将其重新定义为类变量:
class Test:
name = "Harry Banes"
so that you can access name
with just Test.name
: 这样您就可以使用Test.name
来访问name
:
print(Test.name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.