john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

ternary if not none optional parameter isinstance list

# python if statements short circuit BUT be careful using not

a = list()
if not a:
  print 'empty list'

a = None
if not a:
  print 'None'

a = ''
if not a:
    print 'empty string'

# explicit checking can avoid nasty bugs later
if result is not None and len( result ) == 0:


- - - - - - - - - - - - - - - - - - - -
# ternary operator, very useful for optional parameters  def foo( num=None, mylist=None ):
num = num if num else 0     # assign num to 0 by default (if it's None or empty or ...)
mylist = mylist if mylist else []   # store the passed list or assign empty list


assert not isinstance( lst, basestring )        #   input is a list or tuple (not a string)

  • « generator series of values bufferedRWpair
  • UPLOAD »

Published

Jul 3, 2013

Category

python

~104 words

Tags

  • if 6
  • isinstance 1
  • list 23
  • none 1
  • not 11
  • optional 1
  • parameter 6
  • python 180
  • ternary 2