72

I have a python script and I am receiving the following error:

Traceback (most recent call last):
  File "C:\Users\Tim\Desktop\pop-erp\test.py", line 1, in <module>  
    s = Something()
NameError: name 'Something' is not defined

Here is the code that causes the problem:

s = Something()
s.out()

class Something:
    def out():
        print("it works")

This is being run with Python 3.3.0 under Windows 7 x86-64.

Why can't the Something class be found?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
user1899679
  • 839
  • 1
  • 6
  • 4
  • 6
    The solution to this problem is to invoke your classes and functions after you define them. Python does not have any way to forward declare classes or methods so the only option is to put the invocations of functions at the end of the program rather than the beginning. The other option is to put your methods in imported libraries at the top of your file which always get called first. – Eric Leschinski Jan 05 '14 at 02:15

4 Answers4

112

Define the class before you use it:

class Something:
    def out(self):
        print("it works")

s = Something()
s.out()

You need to pass self as the first argument to all instance methods.

Blender
  • 289,723
  • 53
  • 439
  • 496
  • well -- not *all* methods. There's always `@staticmethod` and `@classmethod`, just to keep things interesting :-P – mgilson Feb 11 '13 at 00:22
  • @mgilson For even more fun, `self` *will* work with `@classmethod`, it will just be a misnomer (should be called `cls`). –  Feb 11 '13 at 00:26
  • @delnan -- Yes, of course that's correct. Maybe I shouldn't have added `@classmethod` in there -- but I was only trying to imply that you can change what the first argument is (or even if it gets passed at all). I realize there is nothing magical about the variable name `self` (apart from a very entrenched convention that really shouldn't be violated no matter what). – mgilson Feb 11 '13 at 00:29
  • @mgilson I was not saying you're wrong. And I don't doubt you know all that. It's just another fun fact :-) –  Feb 11 '13 at 00:38
  • This is part of the general principle "Python runs from top to bottom, with namespaces." It starts at the top, in the main namespace, see the line `class Something:`, and parses the class putting `Something` into the namespace. – Charles Merriam Jan 17 '17 at 18:02
23

Note that sometimes you will want to use the class type name inside its own definition, for example when using Python Typing module, e.g.

class Tree:
    def __init__(self, left: Tree, right: Tree):
        self.left = left
        self.right = right

This will also result in

NameError: name 'Tree' is not defined

That's because the class has not been defined yet at this point. The workaround is using so called Forward Reference, i.e. wrapping a class name in a string, i.e.

class Tree:
    def __init__(self, left: 'Tree', right: 'Tree'):
        self.left = left
        self.right = right
Tomasz Bartkowiak
  • 12,154
  • 4
  • 57
  • 62
5

You must define the class before creating an instance of the class. Move the invocation of Something to the end of the script.

You can try to put the cart before the horse and invoke procedures before they are defined, but it will be an ugly hack and you will have to roll your own as defined here:

Make function definition in a python file order independent

Community
  • 1
  • 1
user574435
  • 143
  • 1
  • 10
0

I got the same error below:

NameError: name 'name' is not defined

When I don't define the getter method with @property while the setter and deleter are defined as shown below:

class Person:
    def __init__(self, name):
        self._name = name

    # @property
    # def name(self):
    #     return self._name

    @name.setter
    def name(self, name):
        self._name = name

    @name.deleter # Here
    def name(self):
        del self._name
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129