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

7

16

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

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

Friulian answered 17/7, 2011 at 17:37 Comment(6)
Any particular reason why you don't want False == 0?Repugnant
i totally agree -- but, ironically there is a good side to it -- others get to earn reputation :)Timberland
Apparently this is a good question, since everyone's suggesting answers that aren't quite right.Homestead
What does === mean in this context? Object identity or type and value equality?Teador
Mathematica has an === operator which is a shortcut for the SameQ predicate. But it doesn't make sense in python.Dobson
If you need a ===, you're not writing Python. Python uses duck typing and interfaces rather than typesSiegler
M
16

Try variable is False. False is 0 returns False,

Maziemazlack answered 17/7, 2011 at 17:38 Comment(5)
Not a good answer -- it won't necessarily work with other values.Tinge
@Ethan et al - the OP needs to move the check if he'd like, but I can't remove the post w/o him deselecting it as the answer. It did what he needed at the time though - perhaps he doesn't need explicit type comparison.Maziemazlack
@Ethan Furman: why not? is in python tests object identity, which - as the tags "python", "comparison", "identity" seem to indicate - is exactly what the OP was after. Or am I missing something?Bushing
@Steven, The OP 1) did not explain exactly what he was after, and 2) did not add those tags. So we can't be sure what, exactly, he wants. But if it is the Mathematica meaning, then it is not identity, but type and value. Consequently, a = 98273; b=98273; a === b -> should be True but is will say it's False.Tinge
This is not the answer, is is for object identity. === is for type + value comparison.Mythomania
H
42

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.

Homestead answered 17/7, 2011 at 17:40 Comment(3)
Righto. This is the right answer. That green check needs to move.Corvus
True, False == 1,0 is part of the language spec, so which Python implementation does not do it? Before you write type(x) == type(y) switch to a language that does not use duck typing.Ignaciaignacio
@Jeremy Bank: id(True) == id(True) is part of the spec as well: "The two objects representing the values False and True are the only Boolean objects.".Ignaciaignacio
M
16

Try variable is False. False is 0 returns False,

Maziemazlack answered 17/7, 2011 at 17:38 Comment(5)
Not a good answer -- it won't necessarily work with other values.Tinge
@Ethan et al - the OP needs to move the check if he'd like, but I can't remove the post w/o him deselecting it as the answer. It did what he needed at the time though - perhaps he doesn't need explicit type comparison.Maziemazlack
@Ethan Furman: why not? is in python tests object identity, which - as the tags "python", "comparison", "identity" seem to indicate - is exactly what the OP was after. Or am I missing something?Bushing
@Steven, The OP 1) did not explain exactly what he was after, and 2) did not add those tags. So we can't be sure what, exactly, he wants. But if it is the Mathematica meaning, then it is not identity, but type and value. Consequently, a = 98273; b=98273; a === b -> should be True but is will say it's False.Tinge
This is not the answer, is is for object identity. === is for type + value comparison.Mythomania
T
2

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
Tinge answered 19/9, 2011 at 17:13 Comment(1)
Is it really supposed to be abs(pram1) - abs(pram2) <= delta? It seems like abs(pram1 - pram2) <= delta makes more sense.Homestead
S
1

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

Smallpox answered 17/7, 2011 at 17:39 Comment(0)
M
0

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)))
Mythomania answered 18/9, 2023 at 15:22 Comment(0)
V
0

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.

`

Valse answered 27/11, 2023 at 8:25 Comment(0)
S
0

'===' 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.

Symer answered 17/7 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.