Optimizing methods away or not?
Terry Reedy
tjreedy at udel.edu
Sun Dec 14 16:53:31 EST 2008
Steven D'Aprano wrote:
> I have a class with a method meant to verify internal program logic (not
> data supplied by the caller). Because it is time-consuming but optional,
> I treat it as a complex assertion statement, and optimize it away if
> __debug__ is false:
>
> class Parrot:
> def __init__(self, *args):
> print "Initialising instance..."
> if __debug__:
> self.verify() # check internal program state, not args
> if __debug__:
> def verify(self):
> print "Verifying..."
Given that verify is only called from within _init__, I would put
everything within one 'if __debug__' statement. Either inline
if __debug__:
<code from verify function>
or if for some reason you really don't like that, nested
if __debug__:
def verify():
print "Verifying..."
verify()
tjr
More information about the Python-list
mailing list