[Edu-sig] Gutless classes

Kirby Urner urnerk at qwest.net
Fri Sep 9 00:57:47 CEST 2005


> Extending Classes
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/412717
> 
> --Dethe
> 

Thanks for showing me that.

I guess I'm just fascinated by the mutability of classes at runtime even
without __metaclass__ magic, e.g.:


 >>> class Alter(object):  
        "stub class"
  	  Pass

 >>> def meth1(self): return "Meth 1"   # mindless method 1

 >>> def meth2(self): return "Meth 2"   # I'm mindless too

 >>> class Alter(object):
	pass

 >>> Alter.m1 = meth1
 >>> oa = Alter()  # create objects, go wild
 >>> oa.m1()
 'Meth 1'
 >>> ob = Alter()

Sometime later, rebind the method m1 in the class itself:

 >>> Alter.m1 = meth2
 >>> oa.m1()
 'Meth 2'
 >>> ob.m1()
 'Meth 2'

Honey, the kids seem a little different this morning, don'tcha think?

Pretty lightweight stuff, but "not every language can do it" (tm).

Kirby





More information about the Edu-sig mailing list