Add methods to a class at runtime?

Chris Liechti cliechti at
Sun Sep 1 17:47:01 EDT 2002

"Robert Oschler" <Oschler at> wrote in
news:wmvc9.21757$FJ1.3181934 at 
> 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".

Chris <cliechti at>

More information about the Python-list mailing list