By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

Overriding logical operators?

P: n/a
In Python, you can override the behaviour of most operators for a
class, by defining __add__, __gt__, and the other special object
methods.

I noticed that, although there are special methods for most operators,
they are conspicuously absent for the logical "or" and "and". I'm
guessing that the reason for this is that these operators
short-circuit if their first operand answers the whole question?

Would it be possible to allow overriding the logical operators, with
the caveat that overriding it would prevent short-circuiting?
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In article <ma**************************************@python.o rg>,
Andrew Durdin <ad*****@gmail.com> wrote:
In Python, you can override the behaviour of most operators for a
class, by defining __add__, __gt__, and the other special object
methods.

I noticed that, although there are special methods for most operators,
they are conspicuously absent for the logical "or" and "and". I'm
guessing that the reason for this is that these operators
short-circuit if their first operand answers the whole question?

Would it be possible to allow overriding the logical operators, with
the caveat that overriding it would prevent short-circuiting?


The implementation of "and" and "or" are special, as others have pointed
out. However, you do have some control over how they behave for objects
of user-defined classes. By overriding __nonzero_, you can define
whatever truth value you like for instances of a user-defined class.
See:

http://docs.python.org/lib/truth.html

This would not, in itself, "override" the behaviour of "and" and "or",
in the sense that you were describing, but it would let you control the
outcome for objects of your particular class.

-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
Jul 18 '05 #2

P: n/a
On Sat, 21 Aug 2004 21:38:01 -0400, Michael J. Fromberger
<mi******************@clothing.dartmouth.edu> wrote:

The implementation of "and" and "or" are special, as others have pointed
out. However, you do have some control over how they behave for objects
of user-defined classes. By overriding __nonzero_, you can define
whatever truth value you like for instances of a user-defined class.


I suppose now I ought to say why I need to to do more than that. I've
got a situation where I want to construct an expression, but evaluate
it later when I get more information. I came up with a "DelayedEval"
class, the instances of which can have operations performed on them,
but the result is not evaluated until a particular method is called.
I'm currently overriding all the operators I can, so that these
instances can be handled in a fairly normal fashion for the most part.
For example:

foo = DelayedEval()
foo = (-foo + 15) * 3
foo_positive = (foo >= 0)

print foo.evaluate(10) # prints 15
print foo.evaluate(20) # prints -15
print foo_positive.evaluate(10) # prints True
print foo_positive.evaluate(20) # prints False

In this situation, merely overriding the __nonzero__ method will not
allow me to delay the evaluation of the logical boolean operators; to
do that I need to be able to override them.

The alternative solution that I can see to the issue is to use lambdas
to create the expressions I need, and calling them to evaluate them,
e.g.:

foo = lambda v: (-v + 15) * 3
foo_positive = lambda v: (foo(v) >= 0)

This will work, but having to manually chain the evaluation in this
way is a little awkward. It also doesn't allow me to print the
expressions. With the DelayedEval class, I can do this:

print foo_positive # prints (((-(value) + 15) * 3) >= 0)
Jul 18 '05 #3

P: n/a
Andrew Durdin <ad*****@gmail.com> writes:
In Python, you can override the behaviour of most operators for a
class, by defining __add__, __gt__, and the other special object
methods.

I noticed that, although there are special methods for most operators,
they are conspicuously absent for the logical "or" and "and".
That's because they're not operators, they're flow control constructs.
Well, maybe that's not a helpful answer, but it's one you haven't had
yet :-)
Would it be possible to allow overriding the logical operators, with
the caveat that overriding it would prevent short-circuiting?


I think this would be a foul abomination. But maybe that's just me.
(I don't really approve of operator overloading either).

Cheers,
mwh

--
Ability to type on a computer terminal is no guarantee of sanity,
intelligence, or common sense.
-- Gene Spafford's Axiom #2 of Usenet
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.