Combinations of keywords
Hello, here is my ideas of new keywords combinations : (the code is writed like this #code#. Sorry if I do some english mistakes) 1 - and pass if except This one is just a shortcut of # try : code here except : do something # Its syntaxe is #CODE and DO_SOMETHING if except#, it's more readable i think. It is usefull for code like this : #import A_WINDOW_MODULE and import A_UNIX_MODULE if except If DO_SOMETHING is replaced by #pass# it just skips the ligne (and the error) 2 - CODE if CONDITION else CODE I would like the #VALUE if CONDITION else OTHER_VALUE" can also executes code. Like : #Sign_in(user) if button_i_dont have_a_acount.press() else Log_in(user)# or #break if quit==True# If the #else# isnt present and the condition result is False, the interpreter don't executes that ligne What do you think about ?
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)
On Tue, 21 Feb 2023 at 11:29, Steven D'Aprano <steve@pearwood.info> wrote:
Trying to squeeze many lines of code into one line is a bad idea.
Agreed, in most cases - but there can be (no pun intended.. probably) exceptions to that. if cloudy or raining: print("Bring an umbrella") else: print("Looks fair out") In rare cases, vertical screen space -- and/or even file read and program performance -- can be affected by the number of lines required to represent a module. On Mon, 20 Feb 2023 at 15:03, Otomatyk dupont <otomatyk@gmail.com> wrote:
#import A_WINDOW_MODULE and import A_UNIX_MODULE if except
Here's an attempt to rephrase this slightly, with the following ideas in mind: * Reduce parser ambiguity * Support filtering based on specific exception classes * Rephrase the code to read more closely like natural language
try import A_WINDOW_MODULE but import A_UNIX_MODULE on ImportError
try value = int(x[2]) but value = 0 on IndexError, ValueError
On Tue, 21 Feb 2023 at 12:44, James Addison via Python-ideas < python-ideas@python.org> wrote:
On Mon, 20 Feb 2023 at 15:03, Otomatyk dupont <otomatyk@gmail.com> wrote:
#import A_WINDOW_MODULE and import A_UNIX_MODULE if except
Here's an attempt to rephrase this slightly, with the following ideas in mind:
* Reduce parser ambiguity * Support filtering based on specific exception classes * Rephrase the code to read more closely like natural language
try import A_WINDOW_MODULE but import A_UNIX_MODULE on ImportError
If you really don't want to have the multi-line try...except, this is perfectly possible to implement as a function: # Put this is an "import_utilities" module if you want def best_import(*names): for name in names: try: return __import__(name) except ImportError: continue A_MODULE = best_import("A_WINDOW_MODULE", "A_UNIX_MODULE")
try value = int(x[2]) but value = 0 on IndexError, ValueError
This is basically "exception handling expressions", which have been discussed and rejected many times - see https://peps.python.org/pep-0463/. Personally, I have occasionally wished I could do something like this, but by the time I've thought a bit harder and found a workaround, I usually end up thinking that the workaround is *better* than a solution with an exception handling expression would have been. So although it would be convenient for quick hacks, I basically support the rejection. In the case of this specific example, you can obviously wrap the calculation and exception handling in a function. For a one-off case, that's not worthwhile, and "toy examples" typically look like one-off cases. But in real code, you'll either be doing this a lot (and so the overhead of a function for it is worthwhile) or it will be part of a function where the exception handling will be encapsulated anyway. Paul
On 2023-02-21 11:26, Steven D'Aprano wrote: [snip]
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
I can think of one use-case. Suppose this: try: import windows_module as module except ImportError: import unix_module as module could be shortened to this: import windows_module else unix_module as module Not that I'm in favour of it, because it occurs too rarely to be worth a special syntax. [snip]
participants (5)
-
James Addison
-
MRAB
-
Otomatyk dupont
-
Paul Moore
-
Steven D'Aprano