Python NameError 麻烦
我有一些编程经验,但我对 python 很陌生,我试图弄清楚如何使用和导入 .py 文件中的类而不是 main 。我目前正在使用 netbeans,运行 CPython 3.2.1。
根据我现在的设置,我的所有 .py 文件都位于同一文件夹中。忽略实际内容是什么,它看起来像这样:
pythonprogram.py
from otherfile import *
obj = classB()
print(obj.run())
def method1():
dostuff
otherfile.py
import pythonprogram
class classA:
def __init__(self, thing1=None, thing2=None):
self.thing1 = thing1
self.thing2 = thing2
def run():
pythonprogram.method1()
return something
class classB(classA):
def __init__(self):
super(thing1=None, thing2=None) #this will be more meaningful later
def run():
do some stuff
return super().run()
当我到达创建 obj 的行时,我收到以下错误:
Traceback (more recent call last):
C:\users\me\projects\pythonprogram.py in line 4 in <module>
from room import *
C:\users\me\projects\otherfile.py in line 4 in <module>
import pythonprogram
C:\users\me\projects\pythonprogram.py in line 13 in <module>
obj = classB()
Being unfamiliar with python, 有人可能想让我知道我的现在我想起来,使用 super 是正确的,但这不是重点(而且它肯定不是我现在正在处理的错误)。
我很难找到与我遇到的错误直接相关的教程或另一个问题,但这可能只是因为我对 python 非常不熟悉,以至于当我看到它时我忽略了它;所以如果有人想给我指出正确的教程,那也很好。
否则,我只是想知道我在如何设置一切方面做错了什么,以及我应该如何纠正它。
如果有帮助的话,我首先学习了 Java,也可以使用 C# 和 C++。
I have some programming experience, but I'm very new to python and I'm trying to figure out how to use and import classes from .py files other than the main. I'm currently using netbeans, running CPython 3.2.1.
With my setup right now, all my .py files are in the same folder. Ignoring what the content actually is, it looks something like this:
pythonprogram.py
from otherfile import *
obj = classB()
print(obj.run())
def method1():
dostuff
otherfile.py
import pythonprogram
class classA:
def __init__(self, thing1=None, thing2=None):
self.thing1 = thing1
self.thing2 = thing2
def run():
pythonprogram.method1()
return something
class classB(classA):
def __init__(self):
super(thing1=None, thing2=None) #this will be more meaningful later
def run():
do some stuff
return super().run()
Once I get to the line where I create obj, I get the following error:
Traceback (more recent call last):
C:\users\me\projects\pythonprogram.py in line 4 in <module>
from room import *
C:\users\me\projects\otherfile.py in line 4 in <module>
import pythonprogram
C:\users\me\projects\pythonprogram.py in line 13 in <module>
obj = classB()
Being unfamiliar with python, someone may want to let me know if my use of super is correct, now that I come to think of it, but that's not the point (and its certainly not the error I'm dealing with right now).
I've had a hard time finding a tutorial or another question which directly relates to the error I'm having, but that's probably just because I'm so unfamiliar with python that I'm overlooking it when I see it; so if anyone wants to point me to the right tutorial, that's fine too.
Otherwise, I just would like to know what I'm doing wrong in terms of how I set everything up, and how I should correct it.
If it helps, I learned in Java first and can use C# and C++ as well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的问题是由循环导入引起的。由于事物定义的顺序方式,Python 在这方面不如其他一些语言灵活。在您的情况下,
pythonprogram
需要otherfile
中的classB
才能定义method1
。但是otherfile
需要method1
才能定义classB
!有时,您可以通过将
import
行进一步移至模块下方来解决循环导入问题。但解决这个问题的最好方法是完全避免模块之间的双向依赖。它通常会产生更干净、设计更好的程序。Your problem is caused by a circular import. Python is less flexible than some other languages in this regard, because of the sequential way in which things are defined. In your case,
pythonprogram
requiresclassB
fromotherfile
beforemethod1
can be defined. Butotherfile
requiresmethod1
beforeclassB
can be defined!You can sometimes solve circular imports by moving
import
lines further down a module. But the best way to solve this is to completely avoid bi-directional dependencies between modules. It usually results in cleaner and better-designed programs.如果您希望将 pythonprogram.py 作为模块使用,您可以将其更改如下:
当您运行 pythonprogram.py 时,条件 name == "main" 将为当然,当你从其他模块导入 pythonprogram 时,name == "main" 将为 false。
If you want pythonprogram.py be used as a module, you can change it as follows:
when you run pythonprogram.py, the condition name == "main" will be ture, when you import pythonprogram from other module, name == "main" will be false.
在最基本的层面上,您试图在定义之前调用
method1()
。。本质上,您正在调用
classB
,它又调用classA
,后者尝试调用method1
,但您正在做第一件事,classB()
位于定义method
之前的一行。摆脱这种混乱的最简单方法是将除实际执行操作的代码部分之外的所有内容移至文件的最末尾。
At it's most basic level, you are trying to call
method1()
before you have defined it.Essentially, you are calling
classB
, which in turn callsclassA
, which tries to callmethod1
, but you are doing that first thing,classB()
on a line beforemethod
is defined.The simplest way out of this mess is to just move everything except the part of your code that actually does stuff to the very end of the file.