Attack a sacred Python Cow

Kay Schluehr kay.schluehr at gmx.net
Thu Jul 24 06:40:59 EDT 2008


On 24 Jul., 11:40, Torsten Bronger <bron... at physik.rwth-aachen.de>
wrote:
> Hallöchen!
>
> Bruno Desthuilliers writes:
> > [...]
>
> > How would you handle this case with an implicit 'self' :
>
> > class Foo(object):
> >    pass
>
> > def bar(self):
> >    print self
>
> > Foo.bar = bar
>
> Just like this.  However, the compiler could add "self" to
> non-decorated methods which are defined within "class".

And $self2, $self3, ... to the object methods of nested classes and
$cls2, $cls3, ... to the classmethods of those classes...?

And when we are at it, here is a nice little exercise for the
proponents of compiler magic.

Write a decorator that takes and returns a method and prints the
object the method is bound to. It's very easy to do it when the object
is passed explicitely:

def print_self(func):
    def call(self, *args, **kwd):
        print self
        return func(self, *args, **kwd)
    return call

Conceptual clarity isn't always an entirely bad thing to have.




More information about the Python-list mailing list