6

I have a structure something like this:

def foobar():
    print('FOOBAR!')

class SampleClass:
    foo = foobar
    def printfunc(self):
        self.foo()

This doesn't work because the original foobar function can't handle self being passed to it -- it wasn't part of any class or object to begin with. Python won't let me add the @staticmethod decorator either.

I have no control over the definition of foobar, and I may have to override the value of foo in subclasses.

How can I call foo, without passing the object calling it?

3
  • 1
    Why not just def foo(self): foobar()? Apr 11, 2015 at 17:55
  • 1
    @Huey Because in this specific case, it's not something I can define. It's an external method, and will be overridden with another external method in a subclass. Apr 11, 2015 at 17:55
  • Try to use classmethod. Apr 11, 2015 at 17:56

2 Answers 2

7

Decorators are plain functions, you should be able to call staticmethod(foobar) explicitly in the class definition

class SampleClass:
    foo = staticmethod(foobar)
    def printfunc(self):
        self.foo() # Prints 'FOOBAR!'
0
2

The approach from user2357112's comment seems to work, too:

def foobar():
        print('FOOBAR!')

def foobaz():
        print('FooBAZ!')

class SampleClass:
        def foo(self):
                foobar()

        def printfunc(self):
                self.foo()

class DerivedClass(SampleClass):
        def foo(self):
                foobaz()

sample = SampleClass()
sample.printfunc() # FOOBAR!

derived = DerivedClass()
derived.printfunc() # FooBAZ!

If return values shall make it through, you need return statements on all levels, though:

def foobar():
        print('FOOBAR!')
        return 'foo'

def foobaz():
        print('FooBAZ!')
        return 'baz'

class SampleClass:
        def foo(self):
                return foobar()

        def printfunc(self):
                return self.foo()

class DerivedClass(SampleClass):
        def foo(self):
                return foobaz()

sample = SampleClass()
s = sample.printfunc() # FOOBAR!
print(s) # foo

derived = DerivedClass()
d = derived.printfunc() # FooBAZ!
print(d) # baz
3
  • 1
    It's not simply enough to run the function with a second function, I think. I had to greatly prune the example I gave to its simplest components, but there are some yield statements floating around in there too -- not sure how this would effect them. Apr 11, 2015 at 18:09
  • 2
    Well, while it's appreciated (and expected) that you boil examples down to their relevant parts, make sure that everything that is relevant (to you/to your problem or question) is still included. Einstein's razor is as important as Occam's.
    – das-g
    Apr 11, 2015 at 18:18
  • 1
    @Z̷͙̗̻͖̣̹͉̫̬̪̖̤͆ͤ̓ͫͭ̀̐͜͞ͅͅαлγo: It should work just fine. foobar is a generator function, and foo won't be, but foo should return the generator iterator returned by foobar as if foo were a generator function itself. Apr 11, 2015 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.