473,738 Members | 11,192 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
18 1675
Nicola Mingotti wrote:
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 .


One example of its utility: in a sort algorithm, you would want to be
able to produce consistent orderings of items in a list, even if some of
the items were strings, and others were integers. (That may in fact be
its _only_ utility, for all I know.)

-Peter
Jul 18 '05 #11
>>>>> "Peter" == Peter Maas <fp********@net scape.net> writes:

Peter> 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;
}


Peter> That's not really a conversion. '1' is 0x31 in memory and 0x31 >
Peter> 1.

That's really not a conversion, but not because '1' is 0x31 in memory. It
is because C defines '1' to be equivalent to (in every respect, including
type) the integer to represent it---which is 49 (0x31) in most computers we
use today. So in C (and our current platforms), '1' means 49 and 49 means
'1'. Adding '1' with '1' give you 98, and adding "hello" and '1' gives you
a pointer to the 49-th byte after the beginning of "hello". The size of '1'
is 4. C++ is a bit better in the last respect: '1' is of size one byte
rather than 4 bytes in C++ (so in C++ language, char is a short integer).

Peter> C simply compares memory locations, so comparisons are perhaps
Peter> even be machine dependent.

Untrue. It does not try to compare "memory location". Instead it is simply
comparing based on the values of the two operands. Since C defines '1' to
be its representationa l value which is 49, and 49 is greater than 1, the
comparison result is non-zero.

Peter> Unlike C, Python's comparison results for different types are
Peter> defined in a strange but archicture independent way, see Python
Peter> Library Reference 2.3.3.

Also untrue. Python's comparison result is dependent on implementation, so
even in the same computer, two different Python implementation can give you
different results; and you cannot make any assumption in different
platforms.

Regards,
Isaac.
Jul 18 '05 #12
Isaac To schrieb:
Peter> That's not really a conversion. '1' is 0x31 in memory and 0x31 >
Peter> 1.

That's really not a conversion, but not because '1' is 0x31 in memory. It
is because C defines '1' to be equivalent to (in every respect, including
type) the integer to represent it---which is 49 (0x31) in most computers we
use today.
The mapping of '1' to 0x31 is defined by ASCII which is used by C. If a C
instruction retrieves a char it reads data from an adress and if there is
a 0x31 stored at the adress C treats it as '1'. My short form of this is
"'1' is 0x31 in memory".
Peter> Unlike C, Python's comparison results for different types are
Peter> defined in a strange but archicture independent way, see Python
Peter> Library Reference 2.3.3.

Also untrue. Python's comparison result is dependent on implementation, so
even in the same computer, two different Python implementation can give you
different results; and you cannot make any assumption in different
platforms.
I didn't talk about different programs but different machines (architecture).
Python Standard Library (Comparison) has this

Implementation note: Objects of different types except numbers are
ordered by their type names;

Mit freundlichen Gruessen,

Peter Maas

Regards,
Isaac.

--
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 #13
Peter Hansen <pe***@engcorp. com> wrote in message news:<pd******* *************@p owergate.ca>...
Nicola Mingotti wrote:
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 .


One example of its utility: in a sort algorithm, you would want to be
able to produce consistent orderings of items in a list, even if some of
the items were strings, and others were integers. (That may in fact be
its _only_ utility, for all I know.)


Then, instead of having broken operators in the language, why not
redefine the sort routine?
def _transform(x): .... if isinstance(x, (int, long, float)):
.... return ('', x)
.... elif isinstance(x, complex):
.... return ('', x.real, x.imag)
.... else:
.... return (type(x).__name __, x)
.... def _untransform(tp l): .... if len(tpl) == 3:
.... return complex(tpl[1], tpl[2])
.... else:
.... return tpl[1]
.... def sortedCopy(lst) : .... transformedList = [_transform(x) for x in lst]
.... transformedList .sort()
.... return [_untransform(x) for x in transformedList]
.... sortedCopy([2L, 'spam', 42, 2+1j, 3.14159])

[2L, (2+1j), 3.1415899999999 999, 42, 'spam']
Jul 18 '05 #14
On Thu, Mar 18, 2004 at 08:46:04PM +0100, Peter Maas wrote:
The mapping of '1' to 0x31 is defined by ASCII which is used by C. If a C
instruction retrieves a char it reads data from an adress and if there is
a 0x31 stored at the adress C treats it as '1'. My short form of this is
"'1' is 0x31 in memory".


Wrong. (surely C99 didn't change this)

C doesn't require ASCII as a source-encoding nor as the character
encoding on the target machine.

C's requirements for character set are pretty minimal. The letters and
digits are all required (can someone tell me whether lower-case is
required?), as well as a small set of punctuation, newline, and space.
(Are any of tab, carriage return, vertical tab, or formfeed required?)

This may be the required set of punctuation (the rest are available as
digraphs or trigraphs:
! " % & ' ( ) * + , - . / : ; < = > ? _

My source for the set of punctuation is:
http://csit1cwe.fsu.edu/extra_link/v...f/rulngchr.htm

Jeff

Jul 18 '05 #15
Jeff Epler wrote:
This may be the required set of punctuation (the rest are available as
digraphs or trigraphs:
! " % & ' ( ) * + , - . / : ; < = > ? _

My source for the set of punctuation is:
http://csit1cwe.fsu.edu/extra_link/v...f/rulngchr.htm


Note there's a subtle distinction here; this is the list of required
punctuation _in C source code_. It doesn't have to be the same set of
required punctuation of the character set with which a C program is
running (although obviously usually it will be). Furthermore, the
original poster didn't have anything to say about punctuation at all, so
I'm not even sure why you're bringing this up.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ I do not promise to consider race or religion in my appointments.
I promise only that I will not consider them. -- John F. Kennedy
Jul 18 '05 #16
>>>>> "Peter" == Peter Maas <fp********@net scape.net> writes:

Peter> The mapping of '1' to 0x31 is defined by ASCII which is used by
Peter> C. If a C instruction retrieves a char it reads data from an
Peter> adress and if there is a 0x31 stored at the adress C treats it as
Peter> '1'. My short form of this is "'1' is 0x31 in memory".

In the original post, the literal '1' need not be (and probably is not)
stored in memory at all. In this case you have no "address". But that is
subtle details. The main point is still that, in the world of C, '1' and 49
are completely indistinguishab le *values*. Or put it in another way, in C,
a character literal does *not* represent a "character" . Instead, the
literal '1' is just "the *number* which, by the most common convention used
by the computer hardware, is interpreted as the character representing the
arabic numeral 'one'". It is a *number*. That's the reason why you can add
up 1 and '1'. Not because C "ignores type information and care only the
memory". (Try adding up 2.5 and "1").

It is an important distinction: even in C we talk about the abstraction of
expression, and the abstraction of character literal is a number, not a
"character" in the eye of a regular Python programmer.

Peter> I didn't talk about different programs but different machines
Peter> (architecture). Python Standard Library (Comparison) has this

Peter> Implementation note: Objects of different types except numbers
Peter> are ordered by their type names;

As you said, it is "implementa tion note". Also, about C your argument is
that different platforms can have different encoding in their characters,
and thus different ordering. Since type names are expressed in characters,
it follows directly that names of types in Python also has such dependency.

Regards,
Isaac.
Jul 18 '05 #17
Jeff Epler schrieb:
On Thu, Mar 18, 2004 at 08:46:04PM +0100, Peter Maas wrote:
The mapping of '1' to 0x31 is defined by ASCII which is used by C. If a C
instruction retrieves a char it reads data from an adress and if there is
a 0x31 stored at the adress C treats it as '1'. My short form of this is
"'1' is 0x31 in memory".

Wrong. (surely C99 didn't change this)

C doesn't require ASCII as a source-encoding nor as the character
encoding on the target machine.


ASCII is a recipe to map characters to integers in the range [0,127].
Does C assign the same integer value as ASCII to a character literal
(single printable character or \xhh enclosed in simple quotes)?

If the answer is yes then convert the question to a statement and
you get what I meant. :)

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 #18
Isaac To schrieb:
Peter> I didn't talk about different programs but different machines
Peter> (architecture). Python Standard Library (Comparison) has this

Peter> Implementation note: Objects of different types except numbers
Peter> are ordered by their type names;

As you said, it is "implementa tion note". Also, about C your argument is
that different platforms can have different encoding in their characters,
and thus different ordering. Since type names are expressed in characters,
it follows directly that names of types in Python also has such dependency.


You are right. I give up :)

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 #19

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

Similar topics

13
2499
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
1974
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
1538
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
2453
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
1805
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
1679
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
1364
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
7249
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
3109
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
13308
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
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9335
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...
1
9263
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
9208
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
8210
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
6751
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
4570
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
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2745
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.