473,666 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Override 'and' and 'or'

Is it possible to override 'and' and/or 'or'? I cannot find a special
method for it... __and__ and __rand__ and __or__ and __ror__ are for
binary manipulation... any proposals?

Have marvelous sunday,
Marco

Oct 7 '07 #1
13 1883
Dekker <m.**********@g mail.comwrote:
Is it possible to override 'and' and/or 'or'? I cannot find a special
method for it... __and__ and __rand__ and __or__ and __ror__ are for
binary manipulation... any proposals?
If you want to customize the truth value testing you have to implement
__nonzero__

"
__nonzero__( self)
Called to implement truth value testing, and the built-in operation
bool(); should return False or True, or their integer equivalents 0 or
1. When this method is not defined, __len__() is called, if it is
defined (see below). If a class defines neither __len__() nor
__nonzero__(), all its instances are considered true.
"

Keep in mind the relation between __len__ and __nonzero__

ps. why you need to customize such a thing?

--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair
Oct 7 '07 #2
Dekker a écrit :
Is it possible to override 'and' and/or 'or'? I cannot find a special
method for it... __and__ and __rand__ and __or__ and __ror__ are for
binary manipulation... any proposals?
http://docs.python.org/ref/customization.html
"""
__nonzero__( self)
Called to implement truth value testing, and the built-in operation
bool(); should return False or True, or their integer equivalents 0 or
1. When this method is not defined, __len__() is called, if it is
defined (see below). If a class defines neither __len__() nor
__nonzero__(), all its instances are considered true.
"""

Not that in Python, 'and' don't yield bools:
>>"aa" and "bb"
'bb'
>>"aa" and None # yields None
"aa" and 0
0
>>"aa" or "bb"
'aa'
>>0 or "bb"
'bb'
>>None or "bb"
'bb'
>>"aa" or 0
'aa'
>>"aa" or None
'aa'
>>>
HTH
Oct 7 '07 #3
On Sun, 07 Oct 2007 13:52:15 +0000, Dekker wrote:
Is it possible to override 'and' and/or 'or'?
Not without hacking the Python source code, in which case what you've got
is no longer Python.

Why do you want to do so?
--
Steven.
Oct 7 '07 #4
Dekker wrote:
Is it possible to override 'and' and/or 'or'? I cannot find a special
method for it... __and__ and __rand__ and __or__ and __ror__ are for
binary manipulation... any proposals?

Have marvelous sunday,
Marco
I guess you're looking for __nonzero__()
<URL:http://docs.python.org/ref/customization.h tml>.

You don't actually override the boolean operators, you just tell the
object how to tell others if it evaluates to True or False
(Truth-testing isn't a binary operation; cf. bool(my_object) ).

/W
Oct 7 '07 #5
Wildemar Wildenburger wrote:
[whate everyone else wrote :(]

/W
Dangit! 4th of 4.
Gotta type quicker.

/W
Oct 7 '07 #6
On Sun, 07 Oct 2007 16:24:35 +0200, Wildemar Wildenburger wrote:
Wildemar Wildenburger wrote:
>[whate everyone else wrote :(]

/W

Dangit! 4th of 4.
Gotta type quicker.

That's okay, in two weeks time there will be 139 messages in this thread,
it will have devolved into an argument about whether Python's truth-
testing semantics are better or worse than whatever Java/Lisp/Haskell/
Ruby does, and *then* somebody will respond to the Original Poster with
"customize the __and__ and __or__ methods of your class".

Happens every time.

--
Steven.
Oct 7 '07 #7
On 7 Okt., 16:19, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com .auwrote:
On Sun, 07 Oct 2007 13:52:15 +0000, Dekker wrote:
Is it possible to override 'and' and/or 'or'?

Not without hacking the Python source code, in which case what you've got
is no longer Python.

Why do you want to do so?

--
Steven.
Well I think it is not possible what I wanted to achieve. By
overriding the "and" and "or" keyword I wanted to return a new object:

SqlValueInt(4) and SqlValueInt(5) --SqlOpAnd(SqlVal ueInt(4),
SqlValueInt(5))

This is only possible for: +, -, /, *, >, >=, ...

Well... I have to live with the (binary) __and__, __or__ option and
the user has to write:

SqlValueInt(4) & SqlValueInt(5) --SqlOpAnd(SqlVal ueInt(4),
SqlValueInt(5))

Thanks for your input, but __nonzero__ is not of any help in this
case... I want to abuse the "magic" functions for some transformations
and not some evaluation.

Marco

Oct 7 '07 #8
Dekker wrote:
Well... I have to live with the (binary) __and__, __or__ option and
the user has to write:

SqlValueInt(4) & SqlValueInt(5) --SqlOpAnd(SqlVal ueInt(4),
SqlValueInt(5))

Thanks for your input, but __nonzero__ is not of any help in this
case... I want to abuse the "magic" functions for some transformations
and not some evaluation.
Then again, you could always write a function that does this. I know, I
know ...

/W
Oct 7 '07 #9
On Oct 7, 4:48 pm, Dekker <m.aschwan...@g mail.comwrote:
On 7 Okt., 16:19, Steven D'Aprano <st...@REMOVE-THIS-

cybersource.com .auwrote:
On Sun, 07 Oct 2007 13:52:15 +0000, Dekker wrote:
Is it possible to override 'and' and/or 'or'?
Not without hacking the Python source code, in which case what you've got
is no longer Python.
Why do you want to do so?
--
Steven.

Well I think it is not possible what I wanted to achieve. By
overriding the "and" and "or" keyword I wanted to return a new object:

SqlValueInt(4) and SqlValueInt(5) --SqlOpAnd(SqlVal ueInt(4),
SqlValueInt(5))

This is only possible for: +, -, /, *, >, >=, ...

Well... I have to live with the (binary) __and__, __or__ option and
the user has to write:

SqlValueInt(4) & SqlValueInt(5) --SqlOpAnd(SqlVal ueInt(4),
SqlValueInt(5))

Thanks for your input, but __nonzero__ is not of any help in this
case... I want to abuse the "magic" functions for some transformations
and not some evaluation.

Marco
You can see what "and" and "or" are actually doing:

import dis
dis.dis(lambda: x or y and z)

1 0 LOAD_GLOBAL 0 (x)
3 JUMP_IF_TRUE 11 (to 17)
6 POP_TOP
7 LOAD_GLOBAL 1 (y)
10 JUMP_IF_FALSE 4 (to 17)
13 POP_TOP
14 LOAD_GLOBAL 2 (z)
> 17 RETURN_VALUE
Here you can see nicely that they are not implemented as specialized
opcodes but being compiled to jumps. This causes their lazy nature. If
"and" would be implemented as a normal ( eager ) operator a statement
like:

if l and l[0] == 2:
BLOCK

would raise an IndexError if l is empty.

Oct 7 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2650
by: Ed | last post by:
Greetings! I'm trying to implement own collection BaseCollection (inherited from NameObjectCollectionBase class), but my attempt to override NameObjectCollectionBase virtual method public virtual new System.Collections.IEnumerator GetEnumerator ( ) fails with compiller error: "'bla.bla.bla.BaseCollection.GetEnumerator()' : cannot override inherited member 'System.Collections.Specialized.NameObjectCollection
3
3140
by: Woodmon | last post by:
Example of my CSS follows: <style type="text/css" media="screen"> BODY { color: white } A:link { color: 66CCFF; } A:visited { color: CC66FF; } A:active { color: CC66FF; } A:hover { color: white; background-color: #0099cc; text-decoration: #000000;}
0
3991
by: Craig Schneider | last post by:
// Is there any way to override the XML Serialization of the following SimpleClass // to turn off the DefaultValue of a boolean? Sure, I can override the DefaultValue from // true to false, but that is not what I want/need. I really need to just override the fact // that the developer ever supplied a DefaultValue. Setting an override DefaultValue of null // or "new object()" errors. Any help would be appreciated.
11
21488
by: apex | last post by:
Hi All : How to override the class operator = like C++ ? Best regards !
5
2608
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two classes yClass which inherits xClass. xClass has a virtual method which simply writes a line of text stating its origin, yClass implements the same method which writes a line of text stating its origin also (i.e. "From yClass"). I ran the...
5
9593
by: Stoyan | last post by:
Hi All, I don't understand very well this part of MSDN: "Derived classes that override GetHashCode must also override Equals to guarantee that two objects considered equal have the same hash code; otherwise, Hashtable might not work correctly." Does any one know, why we must also override Equals, Please give my an example:) Thanks, Stoyan
7
4549
by: AWHK | last post by:
How can I force anyone who subclasses my class to override the ToString() method? Andreas :-)
2
4051
by: Adriano Coser | last post by:
Hello. After I converted my .net code to the new VC2005 syntax I started to get C4490 on my ExpandableObjectConverter subclass overrides. The GetProperties method is no longer called by the PropertyGrid when I use my subclass as a type converter. Can anyone tell me what has changed? What's the correct way to override GetProperties method? Here's the code for my class:
3
1336
by: Tony Johansson | last post by:
Hello! I have noticed that you can write this override in two different ways. You can either write in this way public override string ToString() { return something; }
2
1321
by: Your_Persona | last post by:
Is there a way to get effect1 with the method in effect2? ///////////////////////////////////////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program
0
8866
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8638
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.