2

I am trying following code:

In [16]: a,b = 1,2

Above allocates 1 to a and 2 to b as expected and there is no error message:

In [18]: a
Out[18]: 1

In [19]: b
Out[19]: 2

But if I try to compare them together:

In [17]: a,b == 1,2
Out[17]: (1, False, 2)

I get 3 outputs. Where is the problem or what do these 3 outputs mean?

3 Answers 3

5

There is no problem here. It is evaluated like this

(a, (b == 1), 2)

A tuple of three values.

In Python the surrounding parentheses are not necessary to create a tuple if it has more than one elements. So as per Python grammar, this is treated as a tuple. This is documented here,

Except when part of a list or set display, an expression list containing at least one comma yields a tuple. The length of the tuple is the number of expressions in the list. The expressions are evaluated from left to right.

...

The trailing comma is required only to create a single tuple (a.k.a. a singleton); it is optional in all other cases. A single expression without a trailing comma doesn’t create a tuple, but rather yields the value of that expression. (To create an empty tuple, use an empty pair of parentheses: ().)


To achieve what you want, you need to do it like this

(a, b) == (1, 2)
9
  • So how can I evaluate both of them together and get 1 or 2 comparison outputs?
    – rnso
    Jan 24, 2018 at 6:57
  • Can I get 2 comparison outputs while using one == comparison operator?
    – rnso
    Jan 24, 2018 at 6:59
  • Nope. That is not possible. Jan 24, 2018 at 6:59
  • list(map(lambda x,y: x==y, [a,b],[1,2])) ?
    – rnso
    Jan 24, 2018 at 7:00
  • It is not a valid expression. Even if you did list(map(lambda x,y: x==y, [[a,b],[1,2]])), the comparison operator is executed for each inner item in the list. Jan 24, 2018 at 7:03
1
a,b = 1,2

This is multiple assignment where the variables a and b are simultaneously given the values 1 and 2. The right-hand side expressions are evaluated from the left to the right first.

  a,b==1,2

Here only the b==1 is taken. That is why the result is 1,True,2

1

The dis module in the python helps you to see whats going under the hood

>>> from dis import dis

>>> def bo(a=2,b=3):
...     print a,b==2,3
... 
>>> dis(bo)
  2           0 LOAD_FAST                0 (a)
              3 PRINT_ITEM          
              4 LOAD_FAST                1 (b)
              7 LOAD_CONST               1 (2)
             10 COMPARE_OP               2 (==)
             13 PRINT_ITEM          
             14 LOAD_CONST               2 (3)
             17 PRINT_ITEM          
             18 PRINT_NEWLINE       
             19 LOAD_CONST               0 (None)
             22 RETURN_VALUE        
>>> bo()
2 False 3
>>> def bo(a=2,b=3):
...     print a,b==3,2
... 
>>> bo()
2 True 2

if we use a,b==3,2 when a=2 and b=3 the tuple `s middle element will have True value

First a and b are pushed onto the stack the popped of the stack and compared with corresponding values and then made into a tuple .

So in effect the comparison is done in the reverse order

So reversing the order of elements provides us a correct value in the resulting tuple

In short

def bo(a=1,b=2,c=3,d=4):
    print a,b,c,d==4,3,2,1
bo()

will print

1 2 3 True 3 2 1

So we can access the tuples middle element for correct value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.