Python NameError: name is not defined

user1899679 picture user1899679 · Feb 11, 2013 · Viewed 529.8k times · Source

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?

Answer

Blender picture Blender · Feb 11, 2013

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.