Does python support the expression "a = b | 1"???

W

Wenhua Zhao

a = b | 1

a = b if b != nil
else a =1

Is there such expression in python?

Thanks a lot!
 
D

Diez B. Roggisch

Wenhua said:
a = b | 1

a = b if b != nil
else a =1

Is there such expression in python?

Soon there will be, but currently: no. What you are after is a ternary
operator like _?_:_ in C. There have been plenty of discussions about
these - search this NG. Depending on your usecase, either

if b is not None:
a = b
else:
a = 1

or

a = [1,b][b is not None]

or even

a = b or 1

will do the trick.

But PLEASE read the discussions first, to understand the reasons and
implications.

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,801
Messages
2,569,658
Members
45,421
Latest member
DoreenCorn

Latest Threads

Top