Set a flag on the function or a global?

sohcahtoa82 at gmail.com sohcahtoa82 at gmail.com
Mon Jun 15 20:24:37 EDT 2015


On Monday, June 15, 2015 at 4:57:53 PM UTC-7, Steven D'Aprano wrote:
> 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

Using a keyword argument for the edir function is the most intuitive and easy to read, IMO.

Also, if two people are working on the same script, it could create problems if one person wants to filter them, but the other doesn't.  That would create a state that they would both have to monitor and keep setting back and forth, rather than each one just setting an argument on their calls.



More information about the Python-list mailing list