2

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
  • Why is somefunc an instance method? Commented May 30, 2014 at 1:17
  • so... why do you have a staticmethod at all? Like ever in any code? What relationship does cool_function bear on MyKlass? Commented May 30, 2014 at 1:21

2 Answers 2

3

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

7 Comments

I tried it gave me this: TypeError: unbound method somefunc() must be called with MyClass instance as first argument (got str instance instead)
@pdubois, You'd have to add the self back in the somefunc method parameter. I've updated this answer.
@garnertb: I tried, same problem. Did I miss anything?
Yes, the both methods need to be static methods or you need to use an instance of MyClass. The code is now correct.
@pdubois Does nesting somefunc under cool_function works out for you?
|
2

Call it with class.method, for example:

tmp = MyKlass.somefunc(ixname)
answered May 30, 2014 at 1:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.