way to define static method

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Aug 22 15:59:34 EDT 2007


On Wed, 22 Aug 2007 20:34:21 +0100, Eric CHAO wrote:

> I think Python uses a very strange way to define static method in a
> class. Why not make it like this?

What is so strange about it?

> class MyClass:
>     def my_static_method(self):
>         # self should be None as it's a static method
>         # just ignore self
> 
> I'm a newcomer so maybe it's quite naive. But I just wonder why it is
> designed like this.

That method above isn't a static method, so why should `self` be None? 
Static methods are created with the `staticmethod` function which can be
used as decorator:

class MyClass(object):
    @staticmethod
    def my_real_static_method():
        # Look ma, no `self`.  :-)

Static methods are just functions, on classes `classmethod`\s are often
more useful.  They get the class as first argument.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list