Python Forum

Full Version: which is "better" (or more Pythonic)?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
which is "better" (or more Pythonic)?

A.
elif a==120 or a==99:
    ...
B.
elif a in (120,99):
    ...
Beautiful is better than ugly.
B
B
I am for B in slightly modified form (subjective 'readability counts'):

elif a in (99, 120):    # ascending is more natural
However, in case of integers it's important not mistakenly 'mentally parse' it as:

elif a in range(99, 120):
A has repetition (a==) and therefore is not DRY.
Personally, I prefer to use a set because

1. It expresses more clearly that I care only about membership and not where the item exists
2. The worst case time for the look up is O(1) rather than O(n) (which, granted isn't going to matter for small collections!)
(Jan-30-2020, 10:43 PM)Gribouillis Wrote: [ -> ]Beautiful is better than ugly.
Or was it the other way around Think
print(__import__("re").sub('^(\w+)(.*?)(\w+)$',r'\3\2\1',__import__("re").search(r"d_103912\">(.*?)\.(?s)",__import__("requests").get("https://python-forum.io/Thread-which-is-better-or-more-Pythonic--24099").text).group(1).strip()).capitalize())
Output:
Ugly is better than beautiful
Doh
(Jan-30-2020, 10:43 PM)Gribouillis Wrote: [ -> ]Beautiful is better than ugly.
so i should ask "which is more beautiful?" but that might be opinion.

i could do:
if isinstance(a,(int,float)) and chr(int(a)) in "cx":
except that i already know that a can only be an int.