[Python-Dev] Re: Guido's Magic Code was: inline sort option
mcherm at mcherm.com
Thu Oct 30 08:01:18 EST 2003
> If only in the C API, I would like to see just such a universalmethod
> alternative to classmethod. That would allow different behaviors to be
> assigned depending on how the method is called.
And that's exactly why I would be wary of it. One of the GREAT SIMPLICITIES
of Python is that all calls are "the same". Calling a function works
a particular way. Calling a callable object does the same, although
the arguments are passed to __call__. Calling a classmethod does the
same thing. Calling a bound method does the same thing except that the
first argument is curried. Here in the Python community, we think it
is a good thing that one must explicitly name "self" as an argument to
methods, and that any function CAN be made a method of objects.
Now you're proposing a special situation, where what appears to be
a single attribute of a class object is actually TWO functions...
two functions that have the same name but subtly different behavior.
Right now, I presume that if I have:
a = A() # a is an instance of A
x = a.aMethod('abc') # this is line 1
y = A.aMethod(a, 'abc') # this is line 2
that line 1 and line 2 do the same thing. This is a direct consequence
of the fact that methods in Python are just functions with an instance
as the first argument. But your "universalmethod" would break this.
It might be worth breaking it, if the result is some *very* readable
code in a whole variety of situations. And it's certainly okay for
Guido to manually create a class which behaves this way via black
magic (and for most users, descriptor tricks are black magic). But
to make it a regular and supported idiom, I'd want to see much better
evidence that it's worthwhile, because there's an important principle
at risk here, and I wouldn't want to trade away the ability to explain
"methods" in two sentences:
A 'method' is just a function whose first argument is 'self'.
The method is an atribute of the class object, and when it is
called using "a.method(args)", the instance 'a' is passed as
for a cute way of making double use of a few factory functions.
-- Michael Chermside
More information about the Python-Dev