Python rocks

Lately, I was complaining I could not find any class-oriented language supporting some kind of inheritance for static properties.

Well, I found one. This is a little embarrassing as I was considering myself as having had a good-enough look at python. I was wrong. I had missed this, among a lot of other things. Reading the official doc and writing one or two “Hello world” projects is definitely not enough to judge a language. You just cannot know what it is till you haven’t started to actually work with it.

Anyway, look at the code:

# module test
class A(object):
    @classmethod
    def methodA(cls):
        print "I am method A, called from %s" % cls
 
class B(A):
    pass
 
# from python shell
>>> from test import A, B
>>> A.methodA()
I am method A, called from <class 'A'>
>>> B.methodA()
I am method A, called from <class 'B'>

The way it is implemented looks a bit funny at first but makes sense if you think about it and consider its history (in python 2.2, you had to write method = classmethod(method) ).

Python also allows you to define “classic” static methods as well, using the @staticmethod decorator.

  1. No comments yet.

  1. No trackbacks yet.