0

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?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
  • When asking Python questions, is always useful to indicate what error happened in your code. This case can be an easily deduced `NameError` but some other times is not that obvious and it costs you zero effort to add such information. – C2H5OH Apr 10 '12 at 00:03
  • Is your indentation here the same as in your files? It looks like `if f == "test"` and following is meant to be one indent to the right. – lvc Apr 10 '12 at 00:06
  • `new` is a bad choice of name for your module. There is already a (deprecated) builtin module called `new` – John La Rooy Apr 10 '12 at 00:18
  • Just a note, I'd suggest avoiding `from ___ import *` - it's (generally) a bad habit to get into. Instead, import whatever you want to use explicitly, or import the module and use `.`. – Amber Apr 10 '12 at 00:25

1 Answers1

2

You can't do that, as f is in a different namespace.

You need to pass your instance of wow your joj instance. To do this, we first create them the other way around, so c exists to pass into f:

from new import *

c = joj('fds')
f = wow('fgd', c)
f.go()

and then we add the parameter c to wow, storing the reference as self.c and use self instead of f as f doesn't exist in this namespace - the object you are referring to is now self:

class wow(object):
    def __init__(self, start, c):
        self.start = start
        self.c = c

    def go(self):
        print "test test test"
        f = raw_input("> ") 
        if f == "test":
            print "!!"  
            return self.c.vov()  
        else:
            print "nope"    
            return self.go()

class joj(object):
    def __init__(self, start):
        self.start = start
    def vov(self):
       print " !!!!! "

Think of each class and function as a fresh start, none of the variables you define elsewhere fall into them.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
  • You need to do something with `c` in `wow.__init__` if you expect it to be usable in `wow.go`, too. I didn't edit to show this because that actually requires a whole new set of explanation, which I think you're more than capable of writing up :) You might also want to say something about the use of recursion to establish a trivial loop... – Karl Knechtel Apr 10 '12 at 00:10