Re: Short syntax for try/pass

2014-10-14 Thread Chris Angelico
On Wed, Oct 15, 2014 at 1:28 AM, Ned Batchelder  wrote:
> PEP 463 proposes a short syntax for this use case:
> http://legacy.python.org/dev/peps/pep-0463/
>
> I'm not sure what became of it.

Not quite; PEP 463 is about the case where you then want a different
value instead. I'm fairly sure the PEP is in a "virtually rejected"
state, but I nudged about it a couple of times and didn't get any
certain response on the subject.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Short syntax for try/pass

2014-10-14 Thread Ned Batchelder

On 10/14/14 10:08 AM, Leonardo Giordani wrote:

Would it be feasible to propose a short syntax like this?

pass SomeException:
   somecode

where the above example would become:

pass IndexError:
  lst[0] = lst[0] + 1

I could not find if such a syntax has been already discussed elsewhere,
so please let me know if this is the case.

Otherwise, what do you think about it?


PEP 463 proposes a short syntax for this use case: 
http://legacy.python.org/dev/peps/pep-0463/


I'm not sure what became of it.

--
Ned Batchelder, http://nedbatchelder.com

--
https://mail.python.org/mailman/listinfo/python-list


Re: Short syntax for try/pass

2014-10-14 Thread Chris Angelico
On Wed, Oct 15, 2014 at 1:08 AM, Leonardo Giordani
 wrote:
> a lot of times the following pattern pops out in Python code:
>
> try:
>  somecode
> except SomeException:
>  pass
>
> Converting the code to a non-EAFP version, for example
>
> if len(lst) != 0:
>  lst[0] = lst[0] + 1

This could be just "if lst:", but I agree, LBYL is not Python's style
(and isn't always possible anyway).

You can at least squish it up onto less lines, which might look better:

try: lst[0] += 1
except IndexError: pass

Alternatively, you can use this style:

from contextlib import suppress

with suppress(IndexError):
lst[0] += 1

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Short syntax for try/pass

2014-10-14 Thread Leonardo Giordani
Hi all,

a lot of times the following pattern pops out in Python code:

try:
 somecode
except SomeException:
 pass

A very simple example could be if you want to process a list that may be
empty

def process_list(lst):
 try:
  lst[0] = lst[0] + 1
 except IndexError:
  pass

or in more complex cases in which however an exception just signals that
there is nothing to do there.

Converting the code to a non-EAFP version, for example

if len(lst) != 0:
 lst[0] = lst[0] + 1

is in my opinion generally against the Python nature, since it relies on a
specific check on the object, instead of trusting the object as being able
to either satisfy the request or raise an exception. That is, this solution
is non-polymorphic.

In such cases sometimes ABC may help, but generally speaking they it is not
always the case of being an instance of a given ABC or not. The problem
here is if the code raises an exception or not.

Would it be feasible to propose a short syntax like this?

pass SomeException:
  somecode

where the above example would become:

pass IndexError:
 lst[0] = lst[0] + 1

I could not find if such a syntax has been already discussed elsewhere, so
please let me know if this is the case.

Otherwise, what do you think about it?

Thank you

Leonardo



Leonardo Giordani
@tw_lgiordani  - lgiordani.com
My profile on About.me  - My GitHub page

-- 
https://mail.python.org/mailman/listinfo/python-list