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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With