简体   繁体   English

如何在另一个python文件中的方法中使用变量的值?

[英]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 namereturn 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.

相关问题 如何在python中的函数内给变量另一个值? - how can I give a variable another value inside a function in python? 我如何使用一个变量的值在python中调用另一个变量? - How can i use the value of one variable to call another in python? 如何让变量的值成为方法,并让方法在Python 2.7中使用该变量? - How can I have a variable's value be a method, and have a method use that variable in Python 2.7? 如何从另一个 python 文件访问 class 方法中的变量? - How do I access the variable inside a class method from another python file? 我在 python 中有一个方法/函数,我试图在其中获取一个返回值,然后我可以将其用作另一个函数中的变量 - I have a method / function in python in which i'm trying to get a return value which i can then use as a variable in another function 如何在另一个 python 文件中运行 python 文件? - How can I run a python file inside another python file? 如何从 Python 中的另一个文件调用 function 中定义的变量? - How can I call a variable defined inside a function from another file in Python? 如何在另一个文件中使用调用文件循环变量(python) - How can I use the calling file loop variable in another file (python) 如何在具有文件目录参数并在另一个 python 文件中返回数组的方法中使用数组? - How can I make use an array in a method that has parameter of a file directory and that returns an array in another python file? 如何在函数Login()中获取'username'的值,以在另一个python程序上使用它? - How can I get the value of 'username' inside the function Login() to use it on another python program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM