Python NameError troubles
I have some programming experience, but I'm very new to python and I'm trying to figur开发者_JAVA技巧e 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.
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
requires classB
from otherfile
before method1
can be defined. But otherfile
requires method1
before classB
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.
If you want pythonprogram.py be used as a module, you can change it as follows:
from otherfile import *
def method1():
print "dosomthing"
if __name__ == "__main__":
obj = classB()
print(obj.run())
when you run pythonprogram.py, the condition name == "main" will be ture, when you import pythonprogram from other module, name == "main" will be false.
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 calls classA
, which tries to call method1
, but you are doing that first thing, classB()
on a line before method
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.
精彩评论