[英]Python script is returning function not defined error
我正在编写一个python类,并试图从该类中的另一个函数调用一个函数,但是我遇到了一个错误。 NameError: name 'bobfunction' is not defined
。 我对类的调用有效,甚至对方法/函数作业的调用也有效。 当job
尝试调用bobfunction
我收到错误消息。 删除对bobfunction
的调用即可。 那么如何从job函数调用bobfunction?
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'
使用self.bobfunction("test data")
运行它
Python使用关键字self
来引用同一类的类方法和变量。 在其他语言中,它与this
关键字相似。 (虽然不一样)。 如果最终在__init__
函数中定义了一个变量,则还可以在其他函数self.variable_name
其与self.variable_name
一起使用。
您应该尝试使用stuff.bobfunction(self,“ test data”)运行它。由于bobfunction位于类stuff中,因此您需要在函数名称之前写出类名称。 括号内的自我必须发送自我的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.