[Tutor] Problem with calling class methods stored in a list

Tobias Marquardt tm at tobix.eu
Thu Jan 10 11:13:32 CET 2013


Hello,

I have a class with some class methods that are also stored in a list.
Now I have a problem calling these methods.
Essentially the relevant code looks like this:

class MyClass(object):

     @classmethod
     def foo(cls):
         cls.method_list[0]()

     @classmethod
     def bar(cls):
         print("Hello World")

     method_list = [bar]


So foo() takes bar() from the list and tries calls it, which results in
an error:

File "aco/antsystem/test.py", line 11, in foo
     cls.method_list[0]()
TypeError: 'classmethod' object is not callable

I guess the reason is that bar() needs to be called on the class like:
cls.bar(), but how to I achieve this here?
Any suggestions?

Tobias






More information about the Tutor mailing list