Hello and welcome!
Trying to squeeze many lines of code into one line is a bad idea. It
makes it hard to read, and will be impossible for Python where
statements and expressions are different and must be on separate lines.
Extra lines are cheap. Python does not encourage people trying to cram
as much code as possible in one line.
Your first suggestion:
code and something if except
which expands to this:
try:
code
except:
something
encourages the Most Diabolical Python Anti-Pattern:
https://realpython.com/the-most-diabolical-python-antipattern/
If you are using plain `except` like that, you probably should stop,
99.9% of the time it is a very, very bad idea.
> It is usefull for code like this :
> #import A_WINDOW_MODULE and import A_UNIX_MODULE if except
If your code blocks are a single statement, you can write:
try: import windows_module
except ImportError: import unix_module
but most people will say that is ugly and be spread out:
try:
import windows_module
except ImportError:
import unix_module
Your second suggestion if just the "ternary if operator":
CODE if CONDITION else CODE
and already works, so long as the two codes and the condition are
expressions.
result = Sign_in(user) if button_i_dont have_a_acount.press() else
Log_in(user)
Most people will say that when the two expressions are *actions* rather
than *values* it is better to use the if...else statement rather than
trying to squash it into one line.
# Beautiful code :-)
if button_i_dont have_a_acount.press():
Sign_in(user)
else:
Log_in(user)
# Ugly code :-(
if button_i_dont have_a_acount.press(): Sign_in(user)
else: Log_in(user)
# Even more ugly but fortunately this is not allowed :-)
if button_i_dont have_a_acount.press(): Sign_in(user) else: Log_in(user)
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/TUBJ4AJPGSAUBMUGWBSGR4YB777RD5QH/
Code of Conduct: http://python.org/psf/codeofconduct/