Skip to content Skip to sidebar Skip to footer

An Irregular Anomaly In Python Tuple

i create two identical tuples and use is operator on them the answer that should come is false but when i use it in vscode/atom/notepadd++ it comes true but when i use the same cod

Solution 1:

I checked it in linux terminal result is False. It is because a is b will become True when a and b are pointing to same object. is does not compare values of the tuples. If you define b=a then you will get True. You can check this discussion Python "is" statement: what is happening?

Solution 2:

This a change introduced in Python 3.7.0 alpha 4 by change bpo-29469: Move constant folding from bytecode layer to AST layer.

We see different behavior between the command line (REPL) and a script. Each line entered at the command line is compiled and executed when entered. All of a script is compiled at once.

In general, for ordinary comparisons, you should use the equality operator (==) instead of the identity operator (is). For all objects the equality operator has a variety of strategies for completing the comparison (actually each class can provide its own __eq__ operator). The identity operator will only compare the objects' ids.

Post a Comment for "An Irregular Anomaly In Python Tuple"