473,651 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Isn't bool __invert__ behaviour "strange"?

Why subclassing bool from int either __invert__ or __neg__ haven't been
overrided to produce a boolean negation? I suspect backwards
compatibility or something alike, but I still wonder..

And since bool can't be subclassed, to have a type like bool but with
boolean negation what do you suggest? A wrapper maybe?
(I would use it so I can evaluate user-defined boolean expression
creating istances of his/her variables in my namespace and than eval()
his/her expression)

Sep 22 '06 #1
23 1775
Saizan wrote:
Why subclassing bool from int either __invert__ or __neg__ haven't
been overrided to produce a boolean negation?
I wonder what -True or -False should evaluate to.

Regards,
Björn

--
BOFH excuse #297:

Too many interrupts

Sep 22 '06 #2

Bjoern Schliessmann wrote:
Saizan wrote:
Why subclassing bool from int either __invert__ or __neg__ haven't
been overrided to produce a boolean negation?

I wonder what -True or -False should evaluate to.

Regards,
Björn

--
BOFH excuse #297:

Too many interrupts
Well in boolean notation -True == False and -False == True, actually
you may prefer ¬ or a line over the term, but since there's no such
operator in python I think we should use "-" which is also the operator
used by Bool himself in his formulation for negation which was 1-x.
Now that I think of it 1-x should work as a negation in Python, too,
since True == 1 and False == 0, but it would be a little annoying to
write expressions in this way.

Sep 22 '06 #3

Saizan wrote:
Why subclassing bool from int either __invert__ or __neg__ haven't been
overrided to produce a boolean negation? I suspect backwards
compatibility or something alike, but I still wonder..

And since bool can't be subclassed, to have a type like bool but with
boolean negation what do you suggest? A wrapper maybe?
(I would use it so I can evaluate user-defined boolean expression
creating istances of his/her variables in my namespace and than eval()
his/her expression)
The not operator and the bool() builtin produce
boolean results. Since bool is a subclass of int,
all the integer operations will remain integer
operations. This was done for backwards
compatability, and is unlikely to change in the 2.x
series.

I don't remember if this is supposed to change
in 3.0. See PEP 3100 and 3099.

John Roth

John Roth

Sep 22 '06 #4
On Fri, 2006-09-22 at 11:25, Saizan wrote:
Bjoern Schliessmann wrote:
Saizan wrote:
Why subclassing bool from int either __invert__ or __neg__ haven't
been overrided to produce a boolean negation?
I wonder what -True or -False should evaluate to.
Well in boolean notation -True == False and -False == True, actually
you may prefer ¬ or a line over the term, but since there's no such
operator in python [...]
It's called "not":
>>not True
False
>>not False
True

-Carsten
Sep 22 '06 #5
Saizan wrote:
Well in boolean notation -True == False and -False == True,
actually you may prefer ¬ or a line over the term,
(I can't remember reading "-" (minus) for a standard boolean
negation operator anywhere. Even C/C++ uses "!".)
but since there's no such operator in python I think we should
use "-"
Stop! What about the "not" operator? It's Python's operator for
negation.
which is also the operator used by Bool himself in his formulation
for negation which was 1-x.
Boole's original formulation uses some weird mathematical things to
emulate "not", "and" and "or". Focus lies on "mathematic al" here.
Now that I think of it 1-x should work as a negation in Python,
too, since True == 1 and False == 0, but it would be a little
annoying to write expressions in this way.
And "a little" unreadable ;)

Regards,
Björn

--
BOFH excuse #445:

Browser's cookie is corrupted -- someone's been nibbling on it.

Sep 22 '06 #6

John Roth wrote:
The not operator and the bool() builtin produce
boolean results. Since bool is a subclass of int,
all the integer operations will remain integer
operations. This was done for backwards
compatability, and is unlikely to change in the 2.x
series.
Ok, shame on me, I completely overlooked "not" and it surprises myself
because it's not like I haven't used it, I just didn't see "not" as an
operator, maybe because i can't find a __not__ method in bool class.
(Is it hidden somewhere or is computed in some other way?)

(However (not x) whould be as annoying as 1-x even if a little more
readable (if you consider lispish parentheses readable):
Input expression: (not (not x)&(not y)!(not (z|v)))
Maybe direct eval is just the wrong way of doing this, I should look
for or make muParser bindings for Python instead..)

Sep 22 '06 #7
Saizan wrote:
(However (not x) whould be as annoying as 1-x even if a little
more readable (if you consider lispish parentheses readable):
Input expression: (not (not x)&(not y)!(not (z|v)))
Did you notice that you use bitwise AND and OR here? How about

not (not x) and (not y) or (not (z or v))

(or what is "!" supposed to mean?)

BTW, not's binding is stronger than and's (IIRC). So

not (not x) and (not y)

mutates to

x and (not y)

Regards,
Björn

--
BOFH excuse #392:

It's union rules. There's nothing we can do about it. Sorry.

Sep 22 '06 #8
"Saizan" <sa*******@gmai l.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
>
John Roth wrote:
>The not operator and the bool() builtin produce
boolean results. Since bool is a subclass of int,
all the integer operations will remain integer
operations. This was done for backwards
compatabilit y, and is unlikely to change in the 2.x
series.

Ok, shame on me, I completely overlooked "not" and it surprises myself
because it's not like I haven't used it, I just didn't see "not" as an
operator, maybe because i can't find a __not__ method in bool class.
(Is it hidden somewhere or is computed in some other way?)

(However (not x) whould be as annoying as 1-x even if a little more
readable (if you consider lispish parentheses readable):
Input expression: (not (not x)&(not y)!(not (z|v)))
Maybe direct eval is just the wrong way of doing this, I should look
for or make muParser bindings for Python instead..)
What about __nonzero__?

class IsOdd(object):
def __init__(self,n ):
self.val = n

def __nonzero__(sel f):
return self.val % 2

for i in range(4):
if IsOdd(i):
print i,"is odd"
else:
print i,"is even"

Prints:
0 is even
1 is odd
2 is even
3 is odd
-- Paul
Sep 22 '06 #9
Thanks for pointing that out ( the "!" is a misstyped "|"), my classes
of discrete math have warped my mind with a mix of various non-C-style
operators notation, I never use bitwise operation and this is just a
bad day for thinking about things..
However I figured out one thing, Python's logic notation is readable
and complete but not compact. (which is fine for programming, and
that's the aim, isn't it?)

Bjoern Schliessmann wrote:
Saizan wrote:
(However (not x) whould be as annoying as 1-x even if a little
more readable (if you consider lispish parentheses readable):
Input expression: (not (not x)&(not y)!(not (z|v)))

Did you notice that you use bitwise AND and OR here? How about

not (not x) and (not y) or (not (z or v))

(or what is "!" supposed to mean?)

BTW, not's binding is stronger than and's (IIRC). So

not (not x) and (not y)

mutates to

x and (not y)

Regards,
Björn

--
BOFH excuse #392:

It's union rules. There's nothing we can do about it. Sorry.
Sep 22 '06 #10

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

Similar topics

7
4001
by: Kornelius Finkenbein | last post by:
Hello folks! I've got a strange problem with my download-script in conjunction with M$ internet explorer, if the filename I want to link to includes more than one points. In Netscape the problem doesn't exist. For example: input: ... download.php?name=virtualdub_1.4.9.zip
2
2368
by: inonzuk | last post by:
why strange you ask? here goes: I have been working with Php and Mysql for a week without any problems. Today I tried executing a php page that connects to mysql and creates a new database, I got this error: "Fatal error: Call to undefined function mysql_connect() in C:\Program Files\Apache Group\Apache2\test\_debug_tmp.php on line 3". I did some reading on the net and checked all my configuration, and it
10
1981
by: Carlos Ribeiro | last post by:
Hello all. I'm sending this to the list because I would like to know if someone else has ever stumbled across this one, and also because one possible solution is to patch, or simply "decorate", some of the Python libraries functions (a few of the os and os.path calls). Note: if you're going to reply, please read it *all* before doing so -- I've already explored this problem quite a bit, and found no obvious solution to it yet.
2
8918
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input onclick="javaScript:alert('document.forms(0)='+document.forms(0)); parent.myFunc(document.forms(0));" type="button" value="Open" name="Button" ID="Button"> The strange part is that the debug alert says that the document.forms(0) is an object så all seem to be well. But...
0
1761
by: William Wisnieski | last post by:
Hello Everyone: I'm having a very strange problem occurring with my Access 2000 database. I call it the "mystery record." Here's the story: I have a query by form that returns a record set in a datasheet. The user double clicks on a row in that datasheet and a main form (pop up) opens bound to a table with a continuous subform bound to a query.
1
2822
by: Chris Magoun | last post by:
I suddenly received an unexpected error in my project. I have been working on this project for some time without this issue. Nothing has changed in the form that caused the exception. A little experimentation shows that I cannot run ANY .NET web project without getting this error: ---------------------------------------------------------------------------- ---- Object reference not set to an instance of an object.
2
1608
by: Juan Pedro Gonzalez | last post by:
I am a bit unsure about this "Effect"... My code: Public Sub Main() Application.EnableVisualStyles() Application.DoEvents() Dim myMainForm As New FormularioPrincipal Dim mySplash As New MyClass.SplashScreen If mySplash.ShowDialog() = DialogResult.OK Then
13
10378
by: Jen | last post by:
One user of my application is experiencing an exception "input string not in correct format". But it makes no sense where it is occurring. It is occurring when a string from a textbox ("172") is being convert to an Int16 (using Convert.ToInt16). How can that be? There are other text boxes that are used in the identical fashion and they don't generate the exception. All there are many other machines running my application that don't...
1
1839
by: John Kotuby | last post by:
Hi all. I am using VS 2005 and VB.NET. Lately as my Web Application is getting larger, I have been getting strange compiler messages like the following: --------------------------- Compiler Error Message: BC30560: 'controls_user_createquicksearchbar_ascx' is ambiguous in the namespace 'ASP'. Source Error:
0
8802
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...
1
8465
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8579
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
6158
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
5612
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
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
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
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
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.