speg
speg

Reputation: 2019

How do I achieve the effect of the === operator in Python?

How do I achieve the effect of the === operator in Python?

For example, I don't want False == 0 to be True.

Upvotes: 16

Views: 9898

Answers (7)

Raja Ramesh
Raja Ramesh

Reputation: 1

'===' operator will compare both value and type of variable.

You can try this in Python:

x == y and type(x) == type(y)

to achieve similar effect.

Upvotes: 0

Amber MirZa
Amber MirZa

Reputation: 36

Answering in context of JS and Python(3.11.4):

In JS:

  1. '=' is a simple assignment operator, can be used to assign values to variables outside a block(expression like an if else statement) and also within an expression.

    let a = 3;
    let b = 5;
    
    if (a = b){
    console.log(a, b);}
    

The above code will work fine and value of both the variables will be assigned as 5

  1. '==' This operator simply checks operand value and return Boolean result. NOTE: It converts the operand to the same type by default and then compares them. e.g. when comparing '3' and 3, it will convert both operand to same type(number or string), then compare the value, The result for 3 == '3' will be TRUE.

  2. '===' This operator checks the value and also the type of the operand. and returns Boolean result. So, for comparison of '3' === 3 , the result will be FALSE

Now consider Python:

  1. '=' This operator is simply used to assign standalone values to variables. Not within an expression (unlike JS)

    a = 3 
    b = 2
    
    if a = b: 
    print('True')
    

This code will not work and throw 'SyntaxError: invalid syntax'


ANSWER YOU ASKED ===>

  1. '==' this operator in Python will work similar to '===' operator of JS, it will compare values and type of operand and then return Boolean result.

NOTE : Above said, I would also like to mention that, due to difference in Data Types in Python and JS or any other programming languages, the operators tend to differ from one another.

For example,

In Python, Integers and Floats(Decimal Numbers) are two seperate Data Types and in JS they are simply termed as Numbers. Hence the functioning of the operators will vary.


  1. From Python(3.8) onward an new operator ':=' is valid and this operator can be used to assign values within expressions.

`

Upvotes: 0

anilbey
anilbey

Reputation: 1977

Background

There is a lot of misinformation in the answers and comments. is, == and === (the strict equality operator) are all different. In Python, as in many other programming languages, checking for strict equality is a frequent and essential practice.

is operator

is is for checking object identity.

>>> a = 3.4
>>> b = 3.4
>>> a is b
False

== operator

== is for checking the values while enabling type coercion.

>>> 3.0 == 3
True

Here it performed an implicit type conversion.

=== operator

The strict equality operator (implemented as === in some mainstream languages), would return false in the above example. It checks for both the types and the values.

As of today, Python does not implement a single operator to do the string equality comparison.

One common way of implementing it is in Jeremy's answer. This solution works fine if the types and values both match. It does not cover the derived types however.

x == y and type(x) == type(y)

If you want to extend it to the derived types, you should do the following.

x == y and (isinstance(x, type(y)) or isinstance(y, type(x)))

Upvotes: 0

Ethan Furman
Ethan Furman

Reputation: 69021

Going with the Mathematica definition, here's a small function to do the job. Season delta to taste:

def SameQ(pram1, pram2, delta=0.0000001):
    if type(pram1) == type(pram2):
        if pram1 == pram2:
            return True
        try:
            if abs(pram1 - pram2) <= delta:
                return True
        except Exception:
            pass
    return False

Upvotes: 2

If you want to check that the value and type are the same use:

x == y and type(x) == type(y)

In Python, explicit type comparisons like this are usually avoided, but because booleans are a subclass of integers it's the only choice here.


x is y compares identity—whether two names refer to the same object in memory. The Python boolean values are singletons so this will work when comparing them, but won't work for most types.

Upvotes: 42

Alexander Gessler
Alexander Gessler

Reputation: 46607

You can use the is operator to check for object identity. False is 0 will return False then.

Upvotes: 1

g.d.d.c
g.d.d.c

Reputation: 47968

Try variable is False. False is 0 returns False,

Upvotes: 16

Related Questions