[Python-Dev] Re: Guido's Magic Code was: inline sort option

Guido van Rossum guido at python.org
Thu Oct 30 12:16:33 EST 2003

> Darn -- it WOULD be better in some cases if one could ONLY call
> a method on the class, NOT on an instance when the call would in
> any case ignore the instance.  Calling dict.fromkeys(L3) is wonderful,
> the problem is that you can also call it on a dict instance, and THAT
> gets confusing.  Similarly, calling list.sorted(iterable) is wonderful,
> but calling it on a list instance that gets ignored, L1.sorted(iterable),
> could perhaps be confusing.

Let's focus on making this an issue that one learns without much pain.

Given that the most common mistake would be to write a.sorted(), and
that's a TypeError because of the missing argument, perhaps we could
make the error message clearer?

Perhaps we could use a variant of classmethod whose __get__ would
raise the error, rather than waiting until the call -- it could do the
equivalent of the following:

class PickyClassmethod(classmethod):
    def __get__(self, obj, cls):
        if obj is not None:
            raise TypeError, "class method should be called on class only!"
            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().

--Guido van Rossum (home page: http://www.python.org/~guido/)

More information about the Python-Dev mailing list