Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic implementation of quiet / verbose flag for functions

In an effort to write pythonic code, I wonder is there a style guide covering the use of quiet or verbose options for functions.

For example, in my Python package I have a range of functions which call each other, thus it is desirable for the user to be able to request a printed output at times.

For example:

def simple_addition(a, b, silent=True):
    res = a + b
    if not silent: print('The answer is %i' % res)
    return res

Is there a standard arg name here. e.g. Should "quiet" / "silent" be used to suppress all printed outputs. Or should "verbose" be used to demand this if True?

like image 347
IanRoberts Avatar asked Jan 05 '17 02:01

IanRoberts


People also ask

What is the use of verbose in Python?

Verbose is a general programming term for produce lots of logging output. You can think of it as asking the program to "tell me everything about what you are doing all the time". Just set it to true and see what happens.

What is the use of verbose?

In computing, Verbose mode is an option available in many computer operating systems and programming languages that provides additional details as to what the computer is doing and what drivers and software it is loading during startup or in programming it would produce detailed output for diagnostic purposes thus ...

What is verbose in ML?

verbose is the choice that how you want to see the output of your Nural Network while it's training. If you set verbose = 0, It will show nothing.


1 Answers

If you don't want to rely on a logging library, I think your solution is already pythonic enough. It may be a little bit more pythonic to write:

def simple_addition(a, b, silent=True):
    res = a + b
    if not silent:
        print('The answer is %i' % res)
    return res

As stated in PEP 8, Other Recommendations, single-line if statements are okay, but discouraged.

There are other possibilities.

Using or

Using the or operator to encode a condition is arguably not pythonic but personally I think it reads nice: "silent or...", "quiet or...". See below:

def simple_addition(a, b, silent=True):
    res = a + b
    silent or print('The answer is %i' % res)
    return res

The or operator does short-circuit, so print and its arguments are only evaluated when silent is False like when using an if statement.

A disadvantage is that mypy type checking will fail if silent is bound to a boolean type:

$ cat > add.py
def simple_addition(a, b, silent: bool = True):
    res = a + b
    silent or print('The answer is %i' % res)
    return res
^D
$ mypy add.py
add.py:3: error: "print" does not return a value

noop ternary

We could also do this:

def noop(*args, **kwargs):
    pass

def simple_addition(a, b, silent=True):
    _print = noop if silent else print
    res = a + b 
    _print('The answer is %i' % res)
    return res

...but it feels rather unpythonic.

like image 108
Rudy Matela Avatar answered Nov 16 '22 03:11

Rudy Matela