[Python-ideas] Re: Combinations of keywords

2023-02-21 Thread MRAB

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]

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/GUFWKK5B2MS46GUJY3P4PUDO3N2FKBWY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Combinations of keywords

2023-02-21 Thread Paul Moore
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  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
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6JZUNBLQJ4ESPBWNHKGZY3GIC5YNUO67/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Combinations of keywords

2023-02-21 Thread James Addison via Python-ideas
On Tue, 21 Feb 2023 at 11:29, Steven D'Aprano  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  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
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UKAP4NDEWRKI2KC6XBQJOUWQC6CIIQQY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Combinations of keywords

2023-02-21 Thread Steven D'Aprano
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 -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TUBJ4AJPGSAUBMUGWBSGR4YB777RD5QH/
Code of Conduct: http://python.org/psf/codeofconduct/