Add methods to a class at runtime?

Chris Liechti cliechti at gmx.net
Sun Sep 1 19:52:14 EDT 2002


holger krekel <pyth at devel.trillke.net> wrote in
news:mailman.1030921281.8891.python-list at python.org: 

> 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 :-)

robert already posted a link where its mentioned. i think i saw it too in 
that thread.
i might have choosen the wrong words... - it was it _was_ discussed, and as 
it looks, it was in the 2.2a release.

here is an older "Unifying types and classes in Python 2.2", which still 
mentiones __dynamic__, newer versions of it don't:
http://216.239.37.100/search?q=cache:Nld8Sg3osq0C:www.etsimo.uniovi.es/pyth
on/2.2/descrintro.html+__dynamic__&hl=en&ie=UTF-8

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list