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

D-Man dsh8290 at rit.edu
Mon Jun 18 14:17:46 EDT 2001


On Mon, Jun 18, 2001 at 04:51:53PM +0000, Richard Gruet wrote:
| 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..

That depends on how you name your function and what purpose they serve
and what arguments they take.

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

Actually, the ctor doesn't create a new object.  In C++ and Java it is
the 'new' _operator_ that creates the object, then passes it as an
argument to the implicitly called ctor.  Calling, ex, super() doesn't
create another new object, but simply calls a method on the current
object.  In Python calling the class object will create a new
instance, and then implicitly call __init__ handing that instance as
an argument.  The __init__ can then call P.__init__( self )  (assume a
parent class 'P' and the instance is named 'self') which simply
invokes a function and doesn't create another new instance.

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

People in this group have noticed that many of the GoF patterns are
built in to Python, while in C++/Java they are patterns that are much
more involved.  I think it is no coincidence that Python is flexible
and not class-based (but rather object-based with modules being a
significant type of object) whereas C++ and Java are inflexible
(certainly less flexible than Python) and class-based.

| > > getInstanceCount()       # returns the number of instances
| > > getInstanceList()        # returns the list of instances
| > > 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.

They should be global because (maybe) all classes should support that
interface.  It would certainly be useful for profiling.  Those
functions are related to the class you are putting them in, but are
much more general.

| And finally, I wonder why they are class methods in Java ? They

Java has class methods becuase the Java designers don't believe in
classes as the end-all.  They don't believe in all objects, just
the objects that are instances of classes, and the classes themselves
(for example functions aren't objects, nor are "primitive" variables).
Java doesn't have modules, so they must fake it by using the 'class'
construct and adding the 'static' keyword to some methods.  

A class is a template for instance objects.  The template shouldn't
contain extra stuff in it that isn't for the objects being templated.
Modules are nice containers that restrict the "globalness" of
functions and other objects while providing a cohesiveness wrt the
class object.  Modules and classes complement each other very nicely,
and should be used as such.  The 'class' shouldn't be the "only"
construct in a language.


BTW, there has been some discussion recently on adding meta-classes to
Python.  I haven't followed it too closely yet (as I still need to
understand exactly how the concept works), but it may provide the
facilities you are seeking (given your reference to Smalltalk's
metaclasses).

-D





More information about the Python-list mailing list