Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79096

Short syntax for try/pass

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin1!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <giordani.leonardo@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.017
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'syntax': 0.04; 'exception.': 0.09; 'raises': 0.09; 'satisfy': 0.09; 'try:': 0.09; 'url:github': 0.09; 'python': 0.11; 'def': 0.12; 'elsewhere,': 0.16; 'feasible': 0.16; 'indexerror:': 0.16; 'subject:pass': 0.16; 'exception': 0.16; 'all,': 0.19; 'example': 0.22; 'otherwise,': 0.22; 'this?': 0.23; 'case.': 0.24; 'propose': 0.24; 'pass': 0.26; 'subject:/': 0.26; 'code:': 0.26; 'generally': 0.29; 'raise': 0.29; 'converting': 0.30; 'skip:@ 10': 0.30; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'relies': 0.31; 'there.': 0.32; 'cases': 0.33; 'not.': 0.33; 'could': 0.34; 'problem': 0.35; 'except': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'object,': 0.36; 'list': 0.37; 'being': 0.38; 'sometimes': 0.38; 'thank': 0.38; 'version,': 0.38; 'to:addr:python-list': 0.38; 'short': 0.38; 'help,': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'url:about': 0.61; 'profile': 0.61; 'simple': 0.61; 'times': 0.62; 'such': 0.63; 'skip:n 10': 0.64; 'more': 0.64; 'here': 0.66; 'url:me': 0.69; 'abc': 0.84; 'subject:try': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=jGS0WoroCTHHKBjTuLEGBXXzhd1rhTIc6uqfMYbB3sY=; b=BBUQnlu6L+/Yk56CMY3rtSzn1bSBA/hTa7KqTAiO2mzyLi2T2r563XWfH+F08eAkRq 0k4tNWkeETO51SiWi01eU2eXH5bNirq7ezw75w7i4aP8otMccxclTAcF0GvvsJhHRxlj ucE44xbpWf+Zf9dj+R7hoTir+eRfg7OxZ7XEvjrR3/bFy+9Zv3m1AzxiQNr+uLLCeenD n1xQM5Em+vSwgz/doQV7ECXRxlSNV8G3xZunVmb1JO6e/O5jJ3oUHAhx8ygrCvI85vg2 tEbjjxqOE6d0TcWNux4U5cstxwclYYS54Uo82GuDEhFAU34xNyCAajX0ASAb7HkcDgh8 RkBw==
MIME-Version 1.0
X-Received by 10.182.98.168 with SMTP id ej8mr4817999obb.41.1413295715005; Tue, 14 Oct 2014 07:08:35 -0700 (PDT)
Date Tue, 14 Oct 2014 16:08:34 +0200
Subject Short syntax for try/pass
From Leonardo Giordani <giordani.leonardo@gmail.com>
To python-list@python.org
Content-Type multipart/alternative; boundary=047d7b2e46f20e3d3b0505628d62
X-Mailman-Approved-At Tue, 14 Oct 2014 16:09:02 +0200
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14848.1413295744.18130.python-list@python.org> (permalink)
Lines 101
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1413295744 news.xs4all.nl 2911 [2001:888:2000:d::a6]:36410
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79096

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

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 <http://twitter.com/tw_lgiordani> - lgiordani.com
My profile on About.me <http://about.me/leonardo.giordani> - My GitHub page
<https://github.com/lgiordani>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Short syntax for try/pass Leonardo Giordani <giordani.leonardo@gmail.com> - 2014-10-14 16:08 +0200

csiph-web