[Tutor] RE: __and__
Emile van Sebille
Emile van Sebille" <emile@fenx.com
Tue, 19 Oct 1999 07:21:47 -0700
Arne,
I agree that the object ought to define itself, but I don't think you
really want to mess with the keywords 'and', 'not' or 'or'. I played
with trying to define AND, OR and NOT as binding with *, -, +, but
couldn't get it to work. I suspect that bytecodehacks may provide an
answer, but I have no direct experience with it.
If you use unbound methods, you could use AND(a,b) or OR(a,b), etc. I
think that gets close to your intent and is certainly not hard on the
eyes.
Emile van Sebille
emile@fenx.com
-------------------
#------start code------
class Bool:
def __init__(self, list=[]):
self.list = list
def __add__(self, other):
list = self.list + filter(lambda x, y=self.list: x not in y,
other.list)
return Bool(list)
def __sub__(self, other):
list = filter(lambda x, y=(self * other).list: x not in y, (self +
other).list)
return Bool(list)
def __mul__(self, other):
list = filter(lambda x, y=self.list: x in y, other.list)
return Bool(list)
def AND(a,b):
c = a*b
return c
b = Bool([1, 2, 3, 4, 5])
a = Bool([2, 3, 10, 5])
print '(a*b): ',(a * b).list
print ' AND : ',AND(a,b).list
print '(a+b): ',(a + b).list
print '(a-b): ',(a - b).list
#-------end code-------
<snip>
>
> thanks for your solution. The problem is that and, or, not sounds more
> natural for my problem than +, - .
>
> In practice: I have to catinate the results of SQL queries. Say A and
B
> are lists of names returned by independant SQL queries then I want to
> get the intersection with 'AND'
>
> C = A and B
>
> or the any existing name with
>
> C = A or B
>
> or the xor with
>
> C = A bot B
>
> Anyway, I'm a little disappointed because that's againsts philosophy
od
> object oriented programming since an object should be allowed to
defined
> it's complete own world.
>
> tell me if I'm wrong,
>
> Arne
>
>