I have the following code, where from cool_function()
I'd like to call 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")
The result I want it to print out is:
FOODIR, FOO, FOO_NEW
What's the way to do it? Currently it prints this:
tmp = self.somefunc(ixname)
NameError: global name 'self' is not defined
asked May 30, 2014 at 1:13
2 Answers 2
You may want to do this:
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')
answered May 30, 2014 at 1:18
user1129665user1129665
7 Comments
pdubois
I tried it gave me this:
TypeError: unbound method somefunc() must be called with MyClass instance as first argument (got str instance instead)
garnertb
@pdubois, You'd have to add the self back in the somefunc method parameter. I've updated this answer.
pdubois
@garnertb: I tried, same problem. Did I miss anything?
garnertb
Yes, the both methods need to be static methods or you need to use an instance of MyClass. The code is now correct.
|
Call it with class.method, for example:
tmp = MyKlass.somefunc(ixname)
answered May 30, 2014 at 1:18
Comments
lang-py
somefunc
an instance method?cool_function
bear onMyKlass
?