Python Forum

Full Version: which is "better"?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
which is "better"?
    if opt[0].lower()=='-n':
        ...
or:
    if opt[0] in ('-n','-N'):
        ...
?
I like first one. However, I would use argparse.
how do you know my use case fits argparse?  or is it that you know that all your future use caes will?
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