why is there no class (static) methods in Python ?

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Jun 18 13:19:03 CEST 2001


Richard Gruet <rgruet at intraware.com> writes:

> But the reason to choose to define a function as a class method
> rather than a mere (static) function is -obviously- when this
> function is closely related to the class itself, not to one of its
> instances.

Depending on how exactly it is related, provinding a module function
might *still* be the better solution - even if class methods were
available.

> In fact, constructors (and destructors) are class methods, not
> instance methods, but they are handled specially in the language so
> they appear as instance methods..

No, constructors (at least as available in Python, C++, and Java), are
really initialization methods, and thus instance methods. This is
easily seen as they get an object as implicit argument.

> Typical examples of class methods:
> loadInstanceFromStream(aStream)    # Create an instance from its persistent

That is a factory function, something that should be separate from the
class itself.

> getInstanceCount()       # returns the number of instances
> getInstanceList()                                # returns the list of
> getClassName()  or getAnyInfoOntheClass() ......

These ought to be globl functions, taking the class as an
argument. Some of these are really easy to implement in Python:

def get_class_name(x):return x.__name__

Regards,
Martin



More information about the Python-list mailing list