全局初始化Python类?

发布于 2024-12-11 01:37:31 字数 798 浏览 0 评论 0原文

我有两个文件,其中一个是 test.py ,另一个是

import new.py

class Test:

    def __init__(self):
        return
    def run(self):
        return 1

if __name__ == "__main__":
    one=Test()
    one.run()

new.py

class New:
    def __init__(self):
        one.run()

New()

现在,当我运行 python test.py 时,我收到此错误,

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import new.py
  File "/home/phanindra/Desktop/new.py", line 5, in <module>
    New()
  File "/home/phanindra/Desktop/new.py", line 3, in __init__
    one.run()
NameError: global name 'one' is not defined

但我想在我的 New!! 中使用其中一个的实例! 我可以这样做吗?

编辑:

我想访问 new.py 中 test.py 中的变量来执行一些处理并将它们返回给 test.py。这不可能吗?

I have two files, one of the test.py is

import new.py

class Test:

    def __init__(self):
        return
    def run(self):
        return 1

if __name__ == "__main__":
    one=Test()
    one.run()

and new.py

class New:
    def __init__(self):
        one.run()

New()

Now when i run python test.py I get this error,

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import new.py
  File "/home/phanindra/Desktop/new.py", line 5, in <module>
    New()
  File "/home/phanindra/Desktop/new.py", line 3, in __init__
    one.run()
NameError: global name 'one' is not defined

But I want to use this instance of one in my New!!
Can I do this??

edit:

I want to access the variable in test.py in new.py to do some process and give them back to test.py. Isn't this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九歌凝 2024-12-18 01:37:31

如果您希望 New 类使用您创建的 Test 实例,则必须将其作为构造函数的一部分传递。

new.py

class New:
    def __init__(self, one):
        one.run()

test.py

import new

class Test:
    def __init__(self):
        return
    def run(self):
        return 1


if __name__ == "__main__":
    one=Test()
    two = new.New(one);

使用全局变量是一种很好的方法,可以在不知道自己是如何做到的情况下破坏代码。最好显式传递您要使用的引用。

If you want your New class to use the instance of Test you created, you have to pass it in as part of the constructor.

new.py

class New:
    def __init__(self, one):
        one.run()

test.py

import new

class Test:
    def __init__(self):
        return
    def run(self):
        return 1


if __name__ == "__main__":
    one=Test()
    two = new.New(one);

Playing around with globals is a great way to break your code without realizing how you did it. It is better to explicitly pass in the reference you want to use.

╰沐子 2024-12-18 01:37:31

不,你不能。您能得到的最接近的是将您需要的东西传递给构造函数:

class New(object):
    def __init__(self, one):
        one.run()

No, you can't. The closest you can get is to pass the thing you need in to the constructor:

class New(object):
    def __init__(self, one):
        one.run()
梦言归人 2024-12-18 01:37:31

one 定义在 if __name__=='__main__' 块内。
因此,仅当 test.py 作为脚本运行(而不是导入)时,one 才会被定义。

为了让模块 newtest 模块访问 one,您需要将 oneif __name__ block:

test.py:

class Test:
    def __init__(self):
        return
    def run(self):
        return 1

one=Test()

if __name__ == "__main__":
    one.run()

然后通过限定名称 test.one 访问 one:

新.py:

import test

class New:
    def __init__(self):
        test.one.run()

New()

one is defined inside the if __name__=='__main__' block.
Consequently, one will get defined only if test.py is run as a script (rather than imported).

For the module new to access one from the test module, you'll need to pull one out of the if __name__ block:

test.py:

class Test:
    def __init__(self):
        return
    def run(self):
        return 1

one=Test()

if __name__ == "__main__":
    one.run()

Then access one by the qualified name test.one:

new.py:

import test

class New:
    def __init__(self):
        test.one.run()

New()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文