Reputation: 1617
I am writing a python class and trying to call a function from another function in the class but I am running into an error.NameError: name 'bobfunction' is not defined
. My call to the class works, even the call to method/function job works. When job
tries to call bobfunction
I get the error message. removing the call to bobfunction
works. So how do I call the bobfunction from the job function?
class stuff():
def __init__(self):
#setup stuff
def bobfunction(self,junk):
print("should work")
return ''
def job(self,data):
bobfunction('test data')
return 'other junk'
Upvotes: 0
Views: 228
Reputation: 11
You should try to run it with stuff.bobfunction(self,"test data") Because bobfunction was situated in class stuff, you need to write class' name before function's name. Self in parentheses must send the name of self.
Upvotes: 0
Reputation: 1375
Run it with self.bobfunction("test data")
Python uses the keyword self
to refer to class methods and variables of the same class. It is similar to this
keyword in other languages. (Not the same though) . If you end up defining a variable in your __init__
function , you can also use it with self.variable_name
in other functions.
Upvotes: 2