3

I would like to to use service functions in a classmethod, where the service function is defined somewhere else. I want to be dynamic, so I can define different functions in different situations. I've tried this:

def print_a():
    print 'a'

class A:
    func = print_a
    @classmethod
    def apply(cls):
        cls.func()

A.apply()

Yet I receive this error:

unbound method print_a() must be called with A instance as first argument (got nothing instead)

Any ideas how to make it work?

TheNavigat
  • 864
  • 10
  • 30

1 Answers1

6

you can use call

def print_a():
    print 'a'

class A:

    func = print_a.__call__

    @classmethod
    def apply(cls):
        cls.func()

A.apply()

Output

a
Kallz
  • 3,244
  • 1
  • 20
  • 38