Don't be too hard on those who want class methods (Re: Python is wierd!)
Christian Tanzer
tanzer at swing.co.at
Thu Jul 27 02:00:02 EDT 2000
Greg Ewing, <undisclosed address>, wrote:
> Usually when people say that class methods
> are not needed, they're talking about C++ or Java style static
> methods. But Smalltalk-style class methods are a different
> species altogether. In Smalltalk, a class is also an instance
> (of class Class or some subclass thereof), so
>
> aClass frobulate: aBlivet
>
> is dynamically dispatched on the runtime class of aClass,
> just like any other message. There is no direct equivalent of
> this in Python, because classes are not instances, they're a
> different kind of object. The suggestion by Moshe Zadka:
>
> > > class Function:
> > >
> > > def __init__(self, func):
> > > self.func = func
> > >
> > > def __call__(self, *args, **kw):
> > > return apply(self.func, args, kw)
> > >
> > > class SomeClass:
> > >
> > > def class_method(a, b, c):
> > > return a+b+c
> > >
> > > class_method = Function(class_method)
>
> isn't quite the same thing, because there is no 'self'
> in scope in the method which is bound to the receiving
> class object (which might be a subclass of SomeClass,
> so you can't just assume that it's SomeClass).
>
> There are no doubt even more convoluted hacks that could
> be used to fix that, but in 999,999,999 cases out of
> a billion it's probably better to revise your design
> so that you don't need class methods in the first place.
That's the one of the reasons I'm hoping to see first-class
metaclasses in some future Python implementation (and hopefully before
Python 3000).
In practice however, I need class variables much more often than class
methods, and class methods not needing `self' (denoting the class in
question) much more often than class methods needing `self'.
And even than, default arguments offer an easy workaround. So I think
that your 1 ppb is slightly overstated.
--
Christian Tanzer tanzer at swing.co.at
Glasauergasse 32 Tel: +43 1 876 62 36
A-1130 Vienna, Austria Fax: +43 1 877 66 92
More information about the Python-list
mailing list