Programming Python 3rd Edition by Mark Lutz has example code (below) where a variable seems to be assigned with options ("or") but what the criteria for selecting the options is not clear.
The explanation is almost non-existent but I think this is to do with feeding in a variable number of command line argments
Can someone clarify the use of "or" in line:
**self.verbose = self.getopt('-v') or self.getenv('VERBOSE') **
Thank you
import sys, os, traceback
class AppError(Exception): pass # errors raised here
class App: # the root class
def __init__(self, name=None):
self.name = name or self.__class__.__name__ # the lowest class
self.args = sys.argv[1:]
self.env = os.environ
self.verbose = self.getopt('-v') or self.getenv('VERBOSE')
self.input = sys.stdin
self.output = sys.stdout
self.error = sys.stderr # stdout may be piped
def closeApp(self): # not __del__: ref's?
pass # nothing at this level
def help(self):
print self.name, 'command-line arguments:' # extend in subclass
print '-v (verbose)'
##############################
# script environment services
##############################
def getopt(self, tag):
try: # test "-x" command arg
self.args.remove(tag) # not real argv: > 1 App?
return 1
except:
return 0
def getarg(self, tag, default=None):
try: # get "-x val" command arg
pos = self.args.index(tag)
val = self.args[pos+1]
self.args[pos:pos+2] = []
return val
except:
return default # None: missing, no default