Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
which is "better"?
#1
which is "better"?
    if opt[0].lower()=='-n':
        ...
or:
    if opt[0] in ('-n','-N'):
        ...
?
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
I like first one. However, I would use argparse.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
how do you know my use case fits argparse?  or is it that you know that all your future use caes will?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
you always could look at bytecode to see which one you like
import dis

def lower(a):
    a.lower() == 'a'
    
def isin(a):
    a in ['a', 'A']
    
dis.dis(lower)

print()

dis.dis(isin)

output
Quote:  4           0 LOAD_FAST                0 (a)
              2 LOAD_ATTR                0 (lower)
              4 CALL_FUNCTION            0
              6 LOAD_CONST               1 ('a')
              8 COMPARE_OP               2 (==)
             10 POP_TOP
             12 LOAD_CONST               0 (None)
             14 RETURN_VALUE

  7           0 LOAD_FAST                0 (a)
              2 LOAD_CONST               3 (('a', 'A'))
              4 COMPARE_OP               6 (in)
              6 POP_TOP
              8 LOAD_CONST               0 (None)
             10 RETURN_VALUE
99 percent of computer problems exists between chair and keyboard.
Reply


Forum Jump:

User Panel Messages

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