Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any python operator that equivalent to javascript triple equal?

Tags:

python

I tried to do this but fail:

File "<input>", line 1
    1==='1'
       ^
SyntaxError: invalid syntax

Is there any workaround?

like image 466
Rama Jakaria Avatar asked Jun 08 '17 01:06

Rama Jakaria


1 Answers

The ordinary == operator in Python already works much like the === operator in JavaScript, in that it won't do string conversions. However, it does not compare types.

>>> 1 == '1'
False
>>> 1 == 1.0
True
>>> 1 == True
True

So we would say that Python doesn't have an exact equivalent to the JavaScript == or === operators. The way Python uses ==, without a === operator, is the norm. JavaScript (and PHP) are a bit unusual.

This last bit about bool might be a bit surprising, but bool is a subclass of int in Python.

like image 120
Dietrich Epp Avatar answered Sep 21 '22 22:09

Dietrich Epp