Set a flag on the function or a global?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Mon Jun 15 19:57:43 EDT 2015


I have a function in a module which is intended to be used by importing 
that name alone, then used interactively:

    from module import edir
    edir(args)


edir is an enhanced version of dir, and one of the enhancements is that 
you can filter out dunder methods. I have reason to believe that people 
are split on their opinion on whether dunder methods should be shown by 
default or not: some people want to see them, others do not. Since edir 
is meant to be used interactively, I want to give people a setting to 
control whether they get dunders by default or not.

I have two ideas for this, a module-level global, or a flag set on the 
function object itself. Remember that the usual way of using this will be 
"from module import edir", there are two obvious ways to set the global:

import module
module.dunders = False

# -or-

edir.__globals__['dunders'] = False


Alternatively, I can use a flag set on the function object itself:

edir.dunders = False


Naturally you can always override the default by explicitly specifying a 
keyword argument edir(obj, dunders=flag).

Thoughts and feedback? Please vote: a module global, or a flag on the 
object? Please give reasons, and remember that the function is intended 
for interactive use.


-- 
Steven D'Aprano



More information about the Python-list mailing list