Python 3K or Python 2.9?

Chris Mellon arkanes at gmail.com
Wed Sep 12 12:00:03 EDT 2007


On 9/12/07, Alex Martelli <aleax at mac.com> wrote:
> Duncan Booth <duncan.booth at invalid.invalid> wrote:
>    ...
> > As for omitting 'self' from method definitions, at first site you might
> > think the compiler could just decide that any 'def' directly inside a
> > class could silently insert 'self' as an additional argument. This
> > doesn't work though because not everything defined in a class has to be
> > an instance method: static methods don't have a self parameter at all,
> > class methods traditionally use 'cls' instead of 'self' as the name of
> > the first parameter and it is also possible to define a function inside
> > a class block and use it as a function. e.g.
>
> Actually you could do the "magic first-parameter insertion" just when
> returning a bound or unbound method object in the function's __get__
> special method, and that would cover all of the technical issues you
> raise.  E.g.:
>

> > class Weird:
> >    def factory(arg):
> >        """Returns a function based on its argument"""
> >
> >    foo = factory("foo")
> >    bar = factory("bar")
> >    del factory
> >
> > When factory is called, it is a simple function not a method. If it had
>
> Sure, that's because the function object itself is called, not a bound
> or unbound method object -- indeed. factory.__get__ never gets called
> here.
>
> > class C:
> >    def method(self): pass
> >
> > and
> >
> > def foo(self): pass
> > class C: pass
> > C.method = foo
> >
> > both of these result in effectively the same class (although the second
> > one has a different name for the method in tracebacks).
>
> And exactly the same would occur if the self argument was omitted from
> the signature and magically inserted when __get__ does its job.
>

This would mean that mixing functions and methods would have to be
done like you do it in C++, with lots of careful knowledge and
inspection of what you're working with. What would happen to stuff
like inspect.getargspec?

Besides, if self isn't in the argument spec, you know that the very
next thing people will complain about is that it's not implicitly used
for locals, and I'll punch a kitten before I accept having to read
Python code guessing if something is a global, a local, or part of
self like I do in C++.



More information about the Python-list mailing list