What is up with "=="?

czrpb czrpb at yahoo.com
Fri Oct 10 12:26:34 EDT 2003


Hello to Those Who Answered!

Thanks for your great messages.

For a bit of context, I was helping someone debug a cgi script. Their
data was coming from a web page and being compared to a value in a
database. One was an Int and one was a String. The compare always came
up false (0). When he would display the values on the web page
(hackishly) he would see the same numbers and wonder why the compare
was failing.

At the same time, I am working on a short Python intro class and
mention that Python is "strongly typed", giving the example of adding
a string and a number. I show that Python excepts in this case (as
opposed to Perl for example).

I just found it interesting that == did not behave the same and
wondered about that. If I get asked about this I will now have good
answers!

Thanks!! 

  Quentin

--- In python-list at yahoogroups.com, Quentin Crain <czrpb at y...> wrote:
> Hello All!
> 
> Could someone explain (links are good too!) why:
> 
> >>> 1+"1"
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: unsupported operand types for +: 'int' and
> 'str'
> >>> 1=="1"
> 0
> 
> 1  Whos __eq__ or __cmp__ is being called: String's or
> Int's?
> 2  Why IS comparison supported across types?
> 3  The exception + gives makes it sound like there is
> a built-in function + that does not like taking an
> 'int' and a 'string'. Read this way, the built-in ==
> does accept 'int' and 'string'.
> 
> Anyway, I have spent a good 10-15 mins googling for
> things like 'python subclass int' and have not found
> anything useful. (PEP's 252 and 253 did not help me --
> perhaps I misread them?)
> 
> What am I missing!
> 
>   Quentin






More information about the Python-list mailing list