I have a python program and I'm trying to import other python classes and I am getting a NameError:
Traceback (most recent call last):
File "run.py", line 3, in <module>
f = wow('fgd')
NameError: name 'wow' is not defined
This is in file called new.py
:
class wow(object):
def __init__(self, start):
self.start = start
def go(self):
print "test test test"
f = raw_input("> ")
if f == "test":
print "!!"
return c.vov()
else:
print "nope"
return f.go()
class joj(object):
def __init__(self, start):
self.start = start
def vov(self):
print " !!!!! "
This is in file run.py
:
from new import *
f = wow('fgd')
c = joj('fds')
f.go()
What am I doing wrong?
You can't do that, as
f
is in a different namespace.You need to pass your instance of
wow
yourjoj
instance. To do this, we first create them the other way around, so c exists to pass into f:and then we add the parameter
c
towow
, storing the reference asself.c
and useself
instead off
asf
doesn't exist in this namespace - the object you are referring to is now self:Think of each class and function as a fresh start, none of the variables you define elsewhere fall into them.