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 ifs matters performance in program, python may not idea ;) (difference measured in nanoseconds, doesn't matter)


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -