Here is my test code:
class test():
def testing(self, val):
print(val)
test.testing(some_input_variable)
Output:
NameError: name 'some_input_variable' is not defined
I cannot figure out why this does not work. If the function was not inside of the class, it works fine. This specific code is a general form of a piece of code I'm trying to use as a proof of concept for another piece of code.
CodePudding user response:
There's nothing much to explain, really. You didn't define some_input_variable
, so you get a NameError
when you try to use it as the argument to test.testing
. Define it, and that particular error goes away.
>>> class test():
... def testing(self, val):
... print(val)
...
>>> test.testing(some_input_variable)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'some_input_variable' is not defined
>>> some_input_variable = 3
>>> test.testing(some_input_variable)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: testing() missing 1 required positional argument: 'val'
Now you get a TypeError
instead of a NameError
, because test.testing
expects to arguments, because you didn't use an instance of test
to invoke the method. Create the instance, and everything works fine.
>>> test().testing(some_input_variable)
3
CodePudding user response:
You are trying to call the testing method as a staticmethod but haven't defined it as a static method.
class test():
@staticmethod
def testing(input_var):
print(input_var)
test.testing(input_var)