Python Forum
which is "better" (or more Pythonic)?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
which is "better" (or more Pythonic)?
#1
which is "better" (or more Pythonic)?

A.
elif a==120 or a==99:
    ...
B.
elif a in (120,99):
    ...
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Beautiful is better than ugly.
Reply
#3
B
Recommended Tutorials:
Reply
#4
B
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
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!)
Reply
#7
(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
Reply
#8
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  which is "better" (or more Pythonic)? Skaperen 2 2,017 Feb-01-2020, 03:10 PM
Last Post: Skaperen
  which is "better" (or more Pythonic)? Skaperen 8 3,229 Nov-16-2019, 06:46 PM
Last Post: Skaperen
  which is more Pythonic? Skaperen 5 2,781 Jul-16-2019, 01:00 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020