473,698 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'1' + 1 ==> True ???


Hi ,

I obtained this result :
------
'1' > 1 True
------
with python2.2.3 and 2.3.3 .
(Yes , in python 2.2 i get 1 instead of True)

but
------ '1' + 1 TypeError
-------

and
-----------------------------'1'.__gt__._ _doc__

'x.__gt__(y) <==> x>y'
-----------------------------

so i'm a little puzzled :)

Is this a bug or is there a very good reason
to this strange behaviour ?

Thank you all .

bye .

(Please forgive english mistakes)
Jul 18 '05 #1
18 1670
On Thu, 18 Mar 2004 03:45:56 +0100, Nicola Mingotti wrote:
I obtained this result :
------
'1' > 1 True
------
with python2.2.3 and 2.3.3 .
(Yes , in python 2.2 i get 1 instead of True)
This should not be surprising; the values are different, so one will be
"greater than" the other; but comparing them is not of much use.
but
------ '1' + 1 TypeError
-------
This, too, should not be surprising. Adding a character and a number
has no unambiguously correct meaning.
and
-----------------------------'1'.__gt__. __doc__
'x.__gt__(y) <==> x>y'
-----------------------------


Showing that the comparison is pretty much what you'd expect. Again,
though, the result of comparing different types like character and
integer isn't particularly useful.
so i'm a little puzzled :)

Is this a bug or is there a very good reason to this strange behaviour


What behaviour did you expect? Without knowing that it's hard to
explain why the reality is different.

--
\ "Judge: A law student who marks his own papers." -- Henry L. |
`\ Mencken |
_o__) |
Ben Finney <http://bignose.squidly .org/>
Jul 18 '05 #2
Nicola Mingotti wrote:
I obtained this result :
------
'1' > 1 True
------
with python2.2.3 and 2.3.3 .
(Yes , in python 2.2 i get 1 instead of True)
Yes, with the exception of complex numbers, doing comparisons between
objects of different types is valid. The ordering is guaranteed to be
consistent on a particular implementation (e.g., here strings are
considered greater than ints). but you're not guaranteed the behavior
will be the same from implementation to implementation (i.e., it's
perfectly valid for another implementation to consider ints less than
strings).
but
------ '1' + 1 TypeError
-------
That's right, adding strings and integers is a type error because it
doesn't make any sense in Python. Python is strongly typed, so it's
unclear what this operation would mean. Do you mean int('1') + 1 or '1'
+ str(1)?
and
-----------------------------'1'.__gt__._ _doc__
'x.__gt__(y) <==> x>y'
-----------------------------


I don't understand what bearing this snippet has on the previous two.
so i'm a little puzzled :)

Is this a bug or is there a very good reason
to this strange behaviour ?


In a strongly, dynamically typed language like Python, it's helpful to
have relational operators usually work even between objects of different
types, even if the result is not immediately useful, because you often
don't know the type of an object you have and so can't guarantee that
you'll always be comparing apples with apples.

There are other approaches, such as restricting the standard relational
operators to only work with objects of the same type and raise
TypeErrors otherwise, and introduce a new set of relational operators
which are forgiving with respect to incompatible objects of different
types (this is something I did with a project of mine, where `eq'
requires type compatibility (and raises if it is not satisfied) but
`seq' ("safe equality") simply returns false instead of raising an error
with incompatible types. However, this is not the approach that Python
has historically used.

Additionally, since types determine the behavior of operations in a
strongly-typed language like Python, not operations, it's useful for
operations that have multiple meanings (+ can mean arithmetic addition
but also string concatenation) -- possible in a strongly-typed language
-- to reject ambiguous arguments rather than trying to guess what it is
you mean.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Does the true light / Of love come in flashes
-- Sandra St. Victor
Jul 18 '05 #3
On Thu, 18 Mar 2004 13:24:14 +1050, Ben Finney wrote:
On Thu, 18 Mar 2004 03:45:56 +0100, Nicola Mingotti wrote:
I obtained this result :
------
> '1' > 1 True
------ This should not be surprising; the values are different, so one will be
"greater than" the other; but comparing them is not of much use.
Here i expect "Undefined Operation" or, if this is defined,
this means that '1' and 1 are comparable so, either 1 is
seen as string or '1' is seen as a number . But ....

-----------------
> '1' + 1

TypeError
-------
With this result i see that none of the two before mentioned possibility
is possible because, in the first case i would expect ,as result, '11' and
, in the second, i would expect a number . I obtain TypeError so , i think
this is illogical .

This, too, should not be surprising. Adding a character and a number
has no unambiguously correct meaning. Also comparing a string and a character hasn't unambiguos meaning ,
has it ?

-----------------------------
>'1'.__gt__ .__doc__

'x.__gt__(y) <==> x>y'
-----------------------------

Showing that the comparison is pretty much what you'd expect. Again,
though, the result of comparing different types like character and
integer isn't particularly useful.


Here i would expect the definition of '>' between strings .
And the definition of '>' between 'chars' and 'integers' given
that a strange result has been returned .
I know python is growing and free so i don't expect that everything
is documented or everything is correct and that is the reason i asked here.

What behaviour did you expect? Without knowing that it's hard to
explain why the reality is different.


i imagined two possibility :

1) Like CommmonLisp (clisp implementation) -----> A Strinct Type Error
[1]> (> "1" 1)
*** - >: "1" is not a REAL
2) Like C -----> Char is converted to int
#include<stdio. h>
int main(){
printf("%i %i", '1' > 1 , '1' + 1 );
return 0;
}
==> 1 50
Bye .

Excuse me again for mistakes or possible (but unwanted)
rude language .
Jul 18 '05 #4
On Wed, 17 Mar 2004 19:22:26 -0800, Erik Max Francis wrote:
------
>>> '1' > 1 True
------ Yes, with the exception of complex numbers, doing comparisons between
objects of different types is valid. The ordering is guaranteed to be
consistent on a particular implementation (e.g., here strings are
considered greater than ints). ...


This is all what i wanted to know .
Even if i can't see it's utility , at least
for the behaviour of the comparison between strings
and numbers with every string considered greater of every int .

-----------------------------
>>>'1'.__gt__._ _doc__

'x.__gt__(y) <==> x>y'
-----------------------------


I don't understand what bearing this snippet has on the previous two.


Here i was looking for what you told me , so something like :
x.__gt__(y) <==> x>y if x and y are numeric object ,
every string is greater than every number (ErrorObject ,
user define class ... ??) ...
.... may be one day i could add it myself ... ;)

Thank you very much for you help .

Bye .
Jul 18 '05 #5
On Thu, 18 Mar 2004 05:24:26 +0100, Nicola Mingotti wrote:
On Thu, 18 Mar 2004 13:24:14 +1050, Ben Finney wrote:
On Thu, 18 Mar 2004 03:45:56 +0100, Nicola Mingotti wrote:
>> '1' > 1
True
This should not be surprising; the values are different, so one will
be "greater than" the other; but comparing them is not of much use.
Here i expect "Undefined Operation"


It is defined, as you saw from inspecting the '1'.__gt__ attribute.
or, if this is defined, this means that '1' and 1 are comparable
Yes. They're stored in the computer, they have different values, those
values are scalars and thus comparable.
so, either 1 is seen as string or '1' is seen as a number
Neither of those is the case. The string is a string; the number is a
number.

Type conversion when a comparison is done is not necessary, and isn't
done. The values are compared, not converted.
>> '1' + 1
TypeError
With this result i see that none of the two before mentioned
possibility is possible because, in the first case i would expect ,as
result, '11' and , in the second, i would expect a number .


Hopefully my explanation shows why addition gives a TypeError while
comparison does not.

Where comparison is possible and unambiguous between values, it's
permitted.

Where addition is possible and unambiguous between values, it's
permitted.
I obtain TypeError so , i think this is illogical .
I hope my explanation makes the logic clearer.
This, too, should not be surprising. Adding a character and a number
has no unambiguously correct meaning.

Also comparing a string and a character hasn't unambiguos meaning ,
has it ?


Yes: "compare the values as stored in the interpreter". Not a
particularly useful thing to do, but harmless and unambiguous.
>>'1'.__gt_ _.__doc__
'x.__gt__(y) <==> x>y'


Showing that the comparison is pretty much what you'd expect. Again,
though, the result of comparing different types like character and
integer isn't particularly useful.


Here i would expect the definition of '>' between strings .


You've got the definition: "compare the values, return True if x is
greater". There's no need for a string to have its own special
definition, different from other scalars.
And the definition of '>' between 'chars' and 'integers'
A "char" is no different from a string in Python.
given that a strange result has been returned .
What's the strange result, though? One value is greater than the other;
you tested to see which one. The result is either True or False;
neither of those is strange.
I know python is growing and free so i don't expect that everything is
documented or everything is correct and that is the reason i asked
here.
You did the right thing; I hope you're gaining the realisation that
Python isn't doing anything strange or wrong in this case.
What behaviour did you expect?


i imagined two possibility :

1) Like CommmonLisp (clisp implementation) -----> A Strinct Type Error
[1]> (> "1" 1)
*** - >: "1" is not a REAL


Lisp is extremely restrictive of what can be compared with its '>'
operator:
( > "A" "B" ) *** - argument to > should be a real number: "A"

Python allows comparison of any scalar type; whether that's useful or
not is up to the programmer to decide.
2) Like C -----> Char is converted to int
#include<stdio. h>
int main(){
printf("%i %i", '1' > 1 , '1' + 1 );
return 0;
}
==> 1 50
With Python's comparison operators, no conversion needs to happen. The
interpreter has values stored; those values are scalars. If they
differ, one will be greater than the other. That fact can be queried
with the comparison operators. No type conversion needs to occur.

As pointed out in another post, the uselessness of comparing strings and
integers is compounded by the fact that there's no guarantee the same
result will occur on different machines, or different Python
implementations .

Since it doesn't break anything, though, I don't see why Python
shouldn't allow you to do it. Nor do I see why it should be converting
values between types to do comparisons.
Excuse me again for mistakes or possible (but unwanted) rude language


No rude language detected in this or your previous post.

--
\ "I went to a museum where all the artwork was done by children. |
`\ They had all the paintings up on refrigerators." -- Steven |
_o__) Wright |
Ben Finney <http://bignose.squidly .org/>
Jul 18 '05 #6
Nicola Mingotti wrote:
Here i was looking for what you told me , so something like :
x.__gt__(y) <==> x>y if x and y are numeric object ,
every string is greater than every number (ErrorObject ,
user define class ... ??) ...


You're guaranteed there will be some ordering between types, but you
don't know what it is, and it might vary from implementation to
implementation. That is, for example, on a given implementation, either
all strs will be greater than ints, or all ints will be greater than
strs, but you shouldn't rely on either behavior.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ The color is red / Under my shoe
-- Neneh Cherry
Jul 18 '05 #7
Quoth Nicola Mingotti <bl*@libero.it> :
....
| Even if i can't see it's utility ,

My guess is that the most important benefit is that any list
of Python objects can be sorted.

Donn Cave, do**@drizzle.co m
Jul 18 '05 #8
On Thu, 18 Mar 2004 05:50:33 +0000, Donn Cave wrote:
My guess is that the most important benefit is that any list
of Python objects can be sorted.


OH , YES !!
This is a sufficient reason ,for me, to admit
what seemed a very crazy feature .
No i can sleep happy ;)

Thank you all , and good night from Italy .

bye .
Jul 18 '05 #9
Nicola Mingotti schrieb:
2) Like C -----> Char is converted to int
#include<stdio. h>
int main(){
printf("%i %i", '1' > 1 , '1' + 1 );
return 0;
}
That's not really a conversion. '1' is 0x31 in memory
and 0x31 > 1. C simply compares memory locations, so
comparisons are perhaps even be machine dependent.
Unlike C, Python's comparison results for different types
are defined in a strange but archicture independent way, see
Python Library Reference 2.3.3. The important thing is that
objects of different types are not equal.
Excuse me again for mistakes or possible (but unwanted)
rude language .


Sorry, no excuse. :-) Really, please stop excusing, for the
following reasons:

1. It's GREAT that you post in English, as I don't understand Italian.
2. Native speakers also make mistakes.
3. Nobody expects an excuse (I think).

:-)

Mit freundlichen Gruessen,

Peter Maas

--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplu sr.de
-------------------------------------------------------------------
Jul 18 '05 #10

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

Similar topics

13
2495
by: dpj5754 | last post by:
Is there a simple and determinist way to make the difference between the 2 sequences: <XX></XX> and <XX/> The EndElement callback does not provide this information.
8
1970
by: Sue | last post by:
Hello! I am back with another question. Remember I am a new JavaScript student and I am aware that this code does not check for all the possibilities and that as a "NEW" JavaScript student I am not expected to check for everything. At any rate, the problem I am having with the following code is that it does not clear the fields once I press the SEND button. So can anyone here enlighten me as to what is causing the problem.
7
1536
by: Robert Allan Schwartz | last post by:
Why do I get a syntax error below? I don't see why volatile works but unsigned does not work. I'm not looking for an answer of the form, "Because the Standard says so", or "Because the C++ grammer says so"; I'm looking for an explanation of *why* the Standard and/or the grammar say so. Thanks,
2
2448
by: bissatch | last post by:
Hi, I am currently writing a simple PHP program that uses an XML file to output rows for a 'Whats New' page. Once written, I will only require updating the XML file and any pages that use the XML file will get their row content from there. The rows may look some thing similar to this: - Added <a>mailing list</a> functionality to <a>homepage</a> - Competition winners announced, click <a>here</a>
4
1804
by: Andy Leszczynski | last post by:
watch this: http://www.turbogears.org.nyud.net:8090/docs/wiki20/20MinuteWiki.mov or read this: http://www.turbogears.org.nyud.net:8090/docs/wiki2 0/page4.html should not it be: 2 def save(self, pagename, data, submit, new): 3 hub.begin()
4
1676
by: Andrzej Wegrzyn | last post by:
Hi, I had a portal that worked before, and over 5 months period JavaScript errors started to show up on all forms where I have datagrids. Using: IE 6.0, WIN XP, IIS 5.1, Framework 1.1 error: Expected ';'
8
1362
by: active | last post by:
I use quickwatch on (astrThisOne <> "") and it reports: False as it should because astrThisOne reports: "" Yet If (astrThisOne <> "") Then executes the Then clause
3
7243
by: MJ | last post by:
Is there a VB.NET (1.1 or 2.0) equivalent for Excel's ? Below is a copy of an example of the Excel VBA that I have been using to pull some data off a webpage. I've got a couple of simple application that currently live in an Excel workbook as VBA solely for this one function. I'd like to move this to a VB.NET Windows form application that does not rely on Excel.
4
3107
by: jienweiwu | last post by:
The following is correct... #include <iostream> using namespace std; int main() { double BMI,height,weight; cout<<"Welcome to the BMI calculator!\n\n"; cout<<"Enter your weight(kilos): ";
232
13254
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
0
9169
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
8899
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
8871
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...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
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
4371
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...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.