我有以下代码,cool_function()
我要致电somefunc()
class MyKlass:
# This function is to be internally called only
def somefunc(self,text):
return (text + "_NEW")
@staticmethod
def cool_function(ixdirname, ixname):
tmp = self.somefunc(ixname)
print ixdirname, ixname, tmp
return
tmp = MyKlass.cool_function("FOODIR","FOO")
我希望它打印出来的结果是:
FOODIR, FOO, FOO_NEW
这样做的方法是什么? 目前打印出来:
tmp = self.somefunc(ixname)
NameError: global name 'self' is not defined
答案 0 :(得分:3)
您可能希望这样做:
class MyClass:
@staticmethod
def somefunc(text):
return text + '_NEW'
@staticmethod
def cool_function(ixdirname, ixname):
tmp = MyClass.somefunc(ixname)
print((ixdirname, ixname, tmp))
return
MyClass.cool_function('FOODIR', 'FOO')
答案 1 :(得分:2)
使用class.method调用它,例如:
tmp = MyKlass.somefunc(ixname)