[Python-Dev] Re: Guido's Magic Code was: inline sort option
Phillip J. Eby
pje at telecommunity.com
Thu Oct 30 12:51:12 EST 2003
At 09:16 AM 10/30/03 -0800, Guido van Rossum wrote:
>class PickyClassmethod(classmethod):
> def __get__(self, obj, cls):
> if obj is not None:
> raise TypeError, "class method should be called on class only!"
> else:
> return classmethod.__get__(self, None, cls)
>
>I don't want to make this behavior the default behavior, because I
>can see use cases for calling a class method on an instance too,
>knowing that it is a class method; otherwise one would have to write
>the ugly x.__class__.foobar().
If there were a 'classonlymethod()' built-in, I'd probably use it, as I use
classmethods a fair bit (mostly for specialized constructors), but I don't
recall ever desiring to call one via an instance. Do you have an example
of the use cases you see?
Hm. What if your PickyClassmethod were a built-in called 'constructor' or
'factorymethod'? Probably too confining a name, if there are other uses
for class-only methods, I suppose.
More information about the Python-Dev
mailing list