Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python NameError: name is not defined

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?

like image 948
user1899679 Avatar asked Feb 10 '13 23:02

user1899679


People also ask

How do I fix NameError is not defined in Python?

The Python "NameError: function is not defined" occurs when we try to call a function that is not declared or before it is declared. To solve the error, make sure you haven't misspelled the function's name and call it after it has been declared.

What is a NameError in Python?

NameError is a kind of error in python that occurs when executing a function, variable, library or string without quotes that have been typed in the code without any previous Declaration. When the interpreter, upon execution, cannot identify the global or a local name, it throws a NameError.

Why is Python saying my variable is not defined?

A NameError is raised when you try to use a variable or a function name that is not valid. In Python, code runs from top to bottom. This means that you cannot declare a variable after you try to use it in your code. Python would not know what you wanted the variable to do.


1 Answers

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.

like image 159
Blender Avatar answered Oct 06 '22 06:10

Blender