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

Diez B. Roggisch deets at nospam.web.de
Wed Oct 5 11:23:30 EDT 2005


Wenhua Zhao wrote:
> 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



More information about the Python-list mailing list