-4

I'm try to do something like that in Python:

return None if a is None

Instead of having:

if a is None:
   return None

But I get an invalid syntax error.

I tried to use pass:

return None if a is None else pass

But it doesn't work as well. Is there a pythonian way to do it?

Avi
  • 19,035
  • 19
  • 69
  • 108
  • 3
    what do you want to return is `a` is not `None`? – Chris_Rands Oct 17 '19 at 11:15
  • I don't think that's possible in Python. – luis.parravicini Oct 17 '19 at 11:15
  • for this trivial example you're returning `None` either way (since `None` is the implicit return value in the absence of a `return` statement) – Chris_Rands Oct 17 '19 at 11:16
  • Note that by default a function returns `None` in Python (i.e. when not returning anything). Which means that the only thing you may want to do is returning something when the opposite condition is true. – keepAlive Oct 17 '19 at 11:16
  • It's not a duplicate to me, as the answer to the linked post doesn't answer this one. – Corentin Pane Oct 17 '19 at 11:20
  • @CorentinPane the accepted answer shows a work-around for a one-liner, while some of the other answers explain that using the ternary operator w/o ``else`` is simply impossible due to syntax restrictions. I'd say it's the perfect duplicate for this question. – Mike Scotty Oct 17 '19 at 11:24
  • the work-around is work-around in the case of list appending, not a work-around for `return` statement. – Corentin Pane Oct 17 '19 at 11:25
  • 1
    Hm, you are right, the correct duplicate would be https://stackoverflow.com/questions/18669836/is-it-possible-to-write-single-line-return-statement-with-if-statement – Mike Scotty Oct 17 '19 at 11:32
  • @MikeScotty Funny how close these two question match in the code and yet the actual point is quite buried in the comments of one of the answers, not even the accepted one. – norok2 Oct 17 '19 at 11:50

2 Answers2

2
return None if ...

is unconditional return of a conditional value.

If you want conditional return, all what you can change is put it on single line, like that:

if a is None: return
zbusia
  • 501
  • 1
  • 7
1

The syntax:

c = a if condition else b

is a proxy for:

if condition:
    c = a
else:
    c = b

And MUST have both branches of the condition (as detailed in the grammar description from the official documentation and further discussed in the corresponding PEP), which is missing in your code. More discussion on the if/else ternary operator in Python here.


The same holds true for return:

return a if condition else b

is equivalent to:

if condition:
    return a
else:
    return b

In your code, if you do not have instructions after that condition, you could use:

return None if a is None else None

which turns out to be a pretty useless statement, as can be replaced with:

return None

or even just omitted altogether.


Finally:

if a is None:
   return None

is already the Pythonic version of the code. Acceptable alternatives include:

if a is None:
    return
if a is None: return None
if a in None: return

PEP8 Style Guide for Python Code suggests avoiding putting multiple statements on the same line:

Compound statements (multiple statements on the same line) are generally discouraged.

however, is open to small-body one-liners:

sometimes it's okay to put an if/for/while with a small body on the same line

norok2
  • 18,523
  • 3
  • 47
  • 78