In javascript, there are strict comparison operators op1 === op2
and op1 !== op2
that will compare both type and value. Is there a pythonic way of achieving the same thing?
So far I've only been able to come up with the following messy conditionals:
isinstance(op1, type(op2)) and isinstance(op2, type(op1)) and op1 == op2
and
not isinstance(op1, type(op2)) or not isinstance(op2, type(op1)) or op1 != op2
The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.
Equality Operators. Ternary/Conditional Operators. The Typescript has two operators for checking equality. One is == (equality operator or loose equality operator) and the other one is === (strict equality operator). Both of these operators check the value of operands for equality.
== in JavaScript is used for comparing two variables, but it ignores the datatype of variable. === is used for comparing two variables, but this operator also checks datatype and compares two values. Checks the equality of two operands without considering their type. Compares equality of two operands with their types.
The triple equals ( === ) is used for strict equality checking it means both operands should be same type and value then only it returns true otherwise it returns false . Example: 1 === "1" // false.
Your approach would indeed check both value and type. There isn't a different operator in Python.
This having been said, in many cases that's not what you want - in Python's philosophy any object that behaves as a duck should be treated as a duck. You often don't want only dictionaries, you want "mapping-like" objects and so on - as long as the object can be used for the particular task then the code should accept it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With