python improvements (Was: Re: New Language)

Bernhard Herzog herzog at online.de
Tue May 16 18:46:52 EDT 2000


Moshe Zadka <moshez at math.huji.ac.il> writes:

> On 16 May 2000, Bernhard Herzog wrote:
> 
> > In that situation, real class methods would have been useful and cleaner
> > than any other solution. In the end, I fell back to normal class
> > attributes that are computed at class definition time because it works
> > well enough for the time being.
> 
> 
> Let me reiterate the way to get class methods:
> 
> class Function:
> 
> 	def __init__(self, func):
> 		self.func = func
> 
> 	def __call__(self, *args, **kw):
> 		return apply(self.func, args, kw)
> 
> class SomeClass:
> 
> 	def class_method(a, b, c):
> 		return a+b+c
> 	class_method = Function(class_method)
> 
> At the addition of a constant overhead (the "Function" definition), and
> one-line per class method (which you'd need anyway, to signal it is a
> class method), you get a no-brainer solution.

Yes this is indeed a very low overhead implementation of class methods.
It even treats private variables properly.

Perhaps I'll actually use this when I eventually get back to my code.

-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list