Python equivalent of Perl's idiom do this or that, usually known as "or die"?
Asked Answered
N

4

6

IN Perl it's quite common to do things like function() || alternative(). If the first returns false it will run the second one.

How can this be easily implemented in Python?

Update

Examples (pseudocode):

x = func() or raise exeption
x = func() or print(x)
func() or print something

If possible solutions should work with Python 2.5+

Note: There is an implied assumption that you cannot modify the func() to raise exceptions, nor to write wrappers.

Norry answered 26/9, 2011 at 9:11 Comment(2)
80% of the time this is a terrible idea. In perl, this almost always depends on side effects that are implied or concealed in the functions. Also, the Python equivalent only works if exceptions are ignored or silenced. Please provide your Python code where you'd like to use this. It may be hiding more serious exception-handling issues.Triquetrous
While you can make it do this, we should point out that it's unusual in Python code. We usually use exceptions to handle unexpected situations.Tace
W
5

Use or: Python uses short circuit evaluation for boolean expressions:

function() or alternative()

If function returs True, the final value of this expression is determined and alternative is not evaluated at all.

Wojcik answered 26/9, 2011 at 9:14 Comment(5)
Is it possible to do something like result = function() or print(result) ?Norry
You can do this in Python 3.X. In Python 2.X "print" is not a function but a statement. So if function() returns a value which evaluates to False, the result will be printed, and as print returns None, the value of "result" will be "None"Wojcik
@Wojcik Considering the reference to perl, I suspect that what is wanted is something like (result = function()) or (print(result)) I don't think that Python allows these sorts of attrocities.Vudimir
@JamesKanze: yes, this does not work in python as the assignment operation returns no value, as in Perl or C/C++Wojcik
@Wojcik Or formulated more precisely: assignment is not an operator in Python. The presence of an = in a Python statement means that it's an assignment statement, but what is to the left of the = isn't an expression, and an expression can't contain a =.Vudimir
P
2

you can use or:

function() or alternative()

also, there is conditional expression defined in PEP 308:

x = 5 if condition() else 0

Which is sometimes useful in expressions and bit more readable.

Pemba answered 26/9, 2011 at 9:13 Comment(0)
Y
1
function() or alternative()

The mechanism is exactly the same.

Yashmak answered 26/9, 2011 at 9:13 Comment(0)
S
1

try with or:

>>> def bye():
  return 3

>>> print bye() or 342432
3

Unfortunately, this does not work like in Perl, because in Perl, after an assignment like my $c = $d || 45; you have in $c the value 45 if $d is undefined. In Python you get an error NameError: name 'd' is not defined

Szeged answered 26/9, 2011 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.