Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Why does equality comparing an int with a string not throw an error?

Tags:

python

types

In Python 3 the attempt to order a string and an int (e.g. 1 > "1") throws a TypeError. Why does comparing a string with an int for equality not throw an error? (e.g. 1=="1") What is an example where comparing a string with an int makes sense? Why do JavaScript and SQL take a different approach?

Related: How does Python compare string and int?

like image 396
e1i45 Avatar asked May 16 '12 11:05

e1i45


1 Answers

Strength and weakness of languages typing

Typing of language could be strong or weak (loose). The stronger typing language has the less different types could be operated in the same operation. Weakness and strength of language typing don't have exact threshold - some language could have stronger typing then other and weaker than another one. Python typing is much stronger than JS.

== implemented as more of less weakly-typed operation. It can compare different types but you need to have both values of the same type to have a chance to obtain True. a == b #true means a and b is objects of the same type and have the equal values. > < in Python 3 implemented as strongly-typed operation and couldn't be performed on different types.

like image 121
I159 Avatar answered Nov 15 '22 18:11

I159