dynamic functions

Uwe Schmitt uwe at rocksport.de
Tue May 14 13:59:56 EDT 2002


Uwe Mayer <merkosh at hadiko.de> wrote:
| In article <mailman.1021337209.14560.python-list at python.org>, 
| brian at sweetapp.com says...


| To speak cleartext:
| I want a class which is initialised with a function in the constructor, 
| a = MyClass(lambda x: x < 3)
| b = MyClass(lambdy y: y %2 == 0)

| which returns a boolean value.
| Classes of this type support concatenation in the sense of a logical 
| AND, so for: 

| a.concate(b)  

| a(x) should test for (x < 3) and (x %2 == 0).

| How can I do that?


    class dynfunc:

	def __init__(self, fun):
	    self.funlist=[fun]

	def concat(self, newclass):
	    self.funlist+=newclass.funlist 

	def __call__(self, arg):

	    for fun in self.funlist:
		res=fun(arg)
		if res==0: break
		res=1

	    return res

     
    a=dynfunc(lambda x: x<10)
    b=dynfunc(lambda x: x%2==1)

    a.concat(b)
    print a(4)
    print a(3)
	    
greetins, uwe.        


-- 
Dr. rer. nat. Uwe Schmitt      ICQ# 159647634   Uwe.Schmitt at num.uni-sb.de
Universitaet des Saarlandes                     Angewandte Mathematik
Building 36.1 Room 4.17         PO-Box 151150   D-66041 Saarbruecken
Mobile:0177/6806587    Fax:+49(0)681/302-4435   Office:+49(0)681/302-2468



More information about the Python-list mailing list