What a beautiful language
This is my first blog-post using a chiny new off-line blogging software! Anyway, this post will be about Python and one of its ugglier corners.
You probably know that calling the implementation of a method in a superclass is easy. But what about a class-method?
Lets give it a try:
>>> class X(object): ... @classmethod ... def foo(cls): ... return cls.__name__ ... >>> class Y(X): ... @classmethod ... def foo(cls): ... return "NANA: " + X.foo() ... >>> Y.foo() 'NANA: X'
Nah, didn't work, and we couldn't expect it too - we didn't send X.foo any cls parameter. But how could we send it? X.foo does not take a parameter, it is given X implicitly as a first argument. It doesn't work as with ordinary methods...
Note that it is possible to get the "right" result if we didn't override foo:
>>> class Z(X): ... >>> Z.foo() 'Z'
The only solution I came up with (that doesn't involve super()) is this uggly one:
>>> class X(object): ... @classmethod ... def foo(cls): ... return cls.__name__ ... >>> class Y(X): ... @classmethod ... def foo(cls): ... return "NANA: " + X.foo.im_func(cls) ... >>> Y.foo() 'NANA: Y'