[Edu-sig] Design Pattern question

Kirby Urner urnerk at qwest.net
Sun Oct 19 22:43:12 EDT 2003


> Your idea is rather more funky, but what else are class
> variables for?
> 
> Simon Burton.
>

What's also funky is how a shared superclass with no constructor can hold
shared instance methods, as well as shared class variables.

 >>> class Foo:  
       # no constructor

 	 def method1(self, a):
		self.a = a

	
 >>> class Spam(Foo):

	 def __init__(self):
		self.b = 10

	
 >>> o = Spam()
 >>> o.b
 10
 
 >>> o.method1(19)
 >>> o.a
 19

You can even define an instance method outside any class...

 >>> def method2(self, c):
	   self.c = c

... then inject it into the superclass...

 >>> Foo.method2 = method2

and all the children now have it.

 >>> o.method2(11)
 >>> o.c
 11

I guess I'm just re-appreciating how flexible Python is.  Design patterns
that just wouldn't be possible in other languages might have some currency
in Python world.

Kirby





More information about the Edu-sig mailing list