why static methods?

Tim Peters tim.one at comcast.net
Wed Feb 19 16:19:53 EST 2003


[Gerrit Holl]
> What is the purpose of static methods? I do not understand the
> difference with a function outside the class.

Good!  That's because there is no difference.  It's purely a namespace
thing.  Especially if you define multiple related classes in a single
module, say A and B, then A.return_something() and B.return_something() can
be clearer than needing to make up two distinct names at the module global
level.  Defining a static method in the class body also gives it easy access
to whatever "private names" the class may use; writing it outside the class
means you'd have to simulate Python's name-mangling by hand.

it's-as-simple-as-it-sounds-ly y'rs  - tim






More information about the Python-list mailing list