Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does python have a comma operator like C

Tags:

python

in C (and C family of languages) an expression (4+7, 5+2) returns 7. But the same expression in Python would result in a tuple (11, 7)

So does python have a comma operator like C ?

like image 791
treecoder Avatar asked Aug 03 '11 06:08

treecoder


2 Answers

You should use something like this to replace it:

comma_operated = (4+7, 5+2)[-1]

but as noted correctly in the comments, why would you want it? It is used in C or C++ quite seldomly and there are good reasons for that.

like image 52
KillianDS Avatar answered Oct 17 '22 01:10

KillianDS


AFAIK, no. Though you can always simulate this by using two lines instead of one. :-)

x = (call_one(), call_two())

# is almost the same as

call_one()
x = call_two()

# or
x = (call_one(), call_two())[1]
like image 4
Sanjay T. Sharma Avatar answered Oct 17 '22 01:10

Sanjay T. Sharma