Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79911
| From | Martin Kemp <martin.kemp@ingg.com> |
|---|---|
| Subject | Re: Python Style Question |
| Date | 2014-10-29 12:42 +0000 |
| References | <406463d0-1287-49a2-b1ed-08a928db4b94@googlegroups.com> <AC2E1A3CADA66941BB826F325E0EFD0F28C2C1@EX2K10MBX1.ad.london.inspiredbroadcast.net> <CAGH2UmEE2NH6yXY2V49ws=eJuRHZ93Bw+BdfnSLsCpjP3Vra-A@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15297.1414589867.18130.python-list@python.org> (permalink) |
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On 29/10/2014 12:01, Rafael Romero Carmona wrote: > 2014-10-29 12:25 GMT+01:00 Martin Kemp <martin.kemp@ingg.com>: On > 29/10/2014 10:45, Anton wrote: >>>> Let's say I have an incoming list of values *l*. Every >>>> element of *l* can be one of the following options: 1) an >>>> integer value 2) a string in form of '<int_value>', e.g. '7' >>>> 3) a string with a json serialization of an integer value, >>>> e.g. '"7"' 4) something else that should be ignored >>>> >>>> I need to transform this list into another list with values >>>> from options 1)-3) coerced to int. The code below should do >>>> this. >>>> >>>> >>>> Variant 1 === >>>> >>>> values = [] for c in l: # Case 1) or 2) try: c_int = int(c) >>>> except ValueError: pass else: values.add(c_int) continue >>>> >>>> # Case 3) try: c_int = int(json.loads(c)) except ValueError: >>>> pass else: values.add(c_int) continue >>>> >>>> === >>>> >>>> Is this code ugly? Does it follow EAFP? Am I missing >>>> something in language best practice? >>>> >>>> Or maybe below is more preferable way with a nested >>>> try...except clause? >>>> >>>> Variant 2 === values = [] for c in l: # Case 1) or 2) try: >>>> c_int = int(c) except ValueError: # Case 3) try: c_int = >>>> int(json.loads(c)) except ValueError: pass else: >>>> values.add(c_int) continue else: values.add(c_int) continue >>>> === >>>> >>>> Thanks, Anton. >>>> >>>> > > Your first example is perfectly fine and is EAFP > > >> Actually it doesn't work because there is no add function and it >> doesn't catch the TypeError function to ignore other exceptions >> than ValueError. Doesn't it? I tested in Python 2.7.6 and 3.4. > > Personally, I prefer to avoid nesting when it's not necessary. > >> -- https://mail.python.org/mailman/listinfo/python-list Ah ok, style-wise it was fine. - -- Martin Kemp (martin.kemp@ingg.com) -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQEcBAEBAgAGBQJUUOD+AAoJEJ0Re0UIDzSu1KQIAK6aCMOv4VqOjmm/zoQrmzLf UGBCLwtHrnDkbXFAIweTSFiM1uf9TDaRpJqY1IrPbJHI4/EAP0Hu07nyx3V6HgzM /+Wb3DkpjW+JQoVqDSGzE/dTPJcU3/b1/EWWpbu72JHplqz9laEAFt9muWyDPs9u kDgM06mDd50lsi83W3i0H1iGL6YbLtsik+/x4G4mMjdq1o9BvRpUjkIiOx7yJ/BR OYzdudltXGqlXcToufHTU2lUv2C0RoHHNO4kytiLoUekCBdGE+Jy/6gQq/AKQu4G 0RYjCOnKNgugfdmDuHi0julPtTEzc+MdY/CcPob4cyy8RDzfQGklGKHP7f9+SJs= =hjWU -----END PGP SIGNATURE-----
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-10-29 03:42 -0700
Re: Python Style Question Martin Kemp <martin.kemp@ingg.com> - 2014-10-29 11:25 +0000
Re: Python Style Question Rafael Romero Carmona <rafaelromcar@gmail.com> - 2014-10-29 12:09 +0100
Re: Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-10-29 12:09 -0700
Re: Python Style Question Rafael Romero Carmona <rafaelromcar@gmail.com> - 2014-10-29 12:52 +0100
Re: Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-10-29 12:23 -0700
Re: Python Style Question Martin Kemp <martin.kemp@ingg.com> - 2014-10-29 12:42 +0000
Re: Python Style Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-30 22:10 +1100
Re: Python Style Question MRAB <python@mrabarnett.plus.com> - 2014-10-30 12:21 +0000
Re: Python Style Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-31 09:48 +1100
Re: Python Style Question Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-31 04:55 +0000
Re: Python Style Question Roy Smith <roy@panix.com> - 2014-10-30 20:34 -0400
Re: Python Style Question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-31 14:52 +1100
Re: Python Style Question Anton <anton.schattenfeld@gmail.com> - 2014-11-12 12:31 -0800
csiph-web