Add methods to a class at runtime?
holger krekel
pyth at devel.trillke.net
Sun Sep 1 19:00:50 EDT 2002
Chris Liechti wrote:
> "Robert Oschler" <Oschler at earthlink.net> wrote in
> news:wmvc9.21757$FJ1.3181934 at e3500-atl1.usenetserver.com:
> > Some interesting tidbits I
> > found while trying out your suggestions, as you will see in the two
> > ".py" files that follow, I discovered that to create a method you can
> > dynamically add to a class _instance_, you need to omit the 'self'
> > parameter in the method declaration. For a method you add dynamically
> > to a class definition itself, then you must include the 'self'
> > parameter in the method declaration. If not, you will get an argument
> > count mismatch complaint from the interpreter. Fascinating stuff.
>
> that's just the differece between a "function" and a "method". a method
> works on an object and that is passed explicit as first argument. "explicit
> is better than implicit" (you can read this and some other design ideas by
> typing ">>> import this" ;-)
>
> > I'm running 2.1.1, anybody know if 2.2.2 is going "disable" either of
> > these abilities? (class or class instance dynamic method addition). I
> > saw a note about such activities requiring a "dynamic" keyword, which
> > is fine. I'd really miss either of these abilities.
>
> nah. "classic classes" stay the way they are and are available until 3.0
> new style classes, those that inherit from "object" or "type", will need
> the marking as "dynamic".
Can you give a pointer for further explanation? Can't believe it :-)
thanks,
holger
More information about the Python-list
mailing list