IsPython really O-O?

gbreed at cix.compulink.co.uk gbreed at cix.compulink.co.uk
Thu Nov 15 12:44:35 EST 2001


Ken Seehof wrote:

> ... in fact
> here's a recipe describing how to add or replace a method in an existing
> class
> using the 'new' module:
> 
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81982

AIUI, that involves a circular reference.  If anybody wants to try some 
benchmarking, compare it with the following:


dynamicModule = "__dynamic__"

def setDynamicClass(object):
  if object.__class__.__module__ != dynamicModule:
    class klass(object.__class__):
      """dynamic class for hacking"""
    klass.__name__ = object.__class__.__name__
    klass.__module__ = dynamicModule
    object.__class__ = klass

def addMethod(object, function):
  setDynamicClass(object)
  setattr(object.__class__, function.__name__, function)



                      Graham



More information about the Python-list mailing list