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

Richard Gruet rgruet at intraware.com
Mon Jun 18 12:51:53 EDT 2001


Martin von Loewis wrote:

> 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.

Only if the module contains only one class..

> > 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.

Yes, I agree that the concrete purpose of constructors is to initialize
instances. But more generally, and from a semantical point of view, calling a
constructor *creates* a new instance, even if the programmer gains control only
on the init part. Then I'd say that a constructor belongs to the class as well as
to the instance being created.

> > 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.

Yes, I read the GoF too. But a class is a factory of instances, no ?

> > 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__

No, I completely disagree. WHY should they be *global* functions rather than
*class* functions (since they are strictly related to the class) ? You provide no
arguments ! Using the class namespace rather than the module namespace for
class-related functions seems more natural (and object oriented) to me.

And finally, I wonder why they are class methods in Java ? They should listen to
your arguments and remove them ;-)
But I don't want to dispute endlessly. I -and other people- would simply like
that such a feature be available in Python, that's all!

regards,

Richard
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rgruet.vcf
Type: text/x-vcard
Size: 370 bytes
Desc: Card for Richard Gruet
URL: <http://mail.python.org/pipermail/python-list/attachments/20010618/310ccc2e/attachment.vcf>


More information about the Python-list mailing list