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

Hugo Arts hugo.yoshi at gmail.com
Thu Jan 10 12:05:55 CET 2013


On Thu, Jan 10, 2013 at 11:13 AM, Tobias Marquardt <tm at tobix.eu> wrote:

> 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?
>
>
Why do you need the method list at all? if you simply get rid of that and
call cls.bar() you would be rid of the problem entirely. What piece of your
code requires this list? Perhaps we can redesign that to be rid of it.

Hugo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130110/d3ddbce4/attachment.html>


More information about the Tutor mailing list