Python: Replace 'one-line if's with ternary if, good idea or not -
in code i'm maintaining, see 'one-line if' construct such this:
if self.verbose >= 1: print("starting...")
i want replace with:
print("starting...") if self.verbose >= 1 else none
or even:
self.verbose >= 1 , print("starting...")
is idea or bad idea, in terms of performance and/or maintainability?
edit: although allowed, not want make totally one-liner, e.g.,if self.verbose >=1: print("starting...")
i'm not sure why that. think alternatives proposed less readable. (but that's subjective of course)
how removing condition in general? @ least remove repetition of if
.
def print_verbose(self, *args, **kwargs): if self.verbose>=1: print(*args, **kwargs) ... self.print_verbose("starting...")
or migrate using logging
module has support different logging levels. configured logging becomes: (provided logger
created module via logging.getlogger()
)
logger.verbose("starting")
regarding performance... if way write if
s matters performance in program, python may not idea ;) (difference measured in nanoseconds, doesn't matter)