Does Python need a semantics for and/or over generators ? was : 'xor' operator?

Boris Borcic borcis at geneva-link.ch
Mon Apr 15 07:33:05 EDT 2002


Ken Peek wrote:

> I think an 'xor' operator is needed.  The 'xor' operator means
> 'logical-exclusive-or', and returns a 1 or 0.
> 
> Here is some code that does what I want (in all the cases I can think of) for
> 'xor':
> 
> # logical-exclusive-or operator:
> def xor(a,b):
>     return (not a) ^ (not b)
> 


For the shortcut semantics, we'd like to have :
     a xor b

behave exactly like :
     (not b and a) or (not a and b)

In Lisp, we would implement that with a special form
macro.

On another hand, the shortcut semantics of test expressions
(and I guess it extends to cases like a < b < c) seems
to flourish to meaningful or at least interesting generalizations,
when terms are generators or iterators.

Indeed, shouldn't two generators g1 and g2, when combined
in an expression of the form e.g. "g1 and g2" or "g1 or g2",
combine meaningfully to become a third generator ? Respectively
one that returns a successive value of g2 for every value that
g1 returns, until one of them returns IterationError, and one
that concatenates the values of g1 with the values of g2 ?

Regards, Boris Borcic
--
"Art is a trigram"




More information about the Python-list mailing list