Set a flag on the function or a global?

Steven D'Aprano steve at pearwood.info
Wed Jun 17 10:59:08 EDT 2015


On Wed, 17 Jun 2015 09:51 am, Cameron Simpson wrote:

> On 16Jun2015 18:18, Steven D'Aprano <steve+comp.lang.python at pearwood.info>
> wrote:
>>On Tuesday 16 June 2015 10:35, MRAB wrote:
>>> On 2015-06-16 01:24, sohcahtoa82 at gmail.com wrote:
>>>> Using a keyword argument for the edir function is the most intuitive
>>>> and easy to read, IMO.
>>
>>edir() has a keyword argument: edir(x, dunders=False) suppresses the
>>return of dunder names. But since the primary purpose of [e]dir is, in my
>>opinion, to be used interactively, needing to type an extra 15 characters
>>to hide dunders is too inconvenient.
> 
> I'm just catching up here, but have now read the whole thread.

Thanks for doing so before commenting :-)


> I am personally against a global (==> single variable affecting all
> callers) of any kind, be it a function attribute or whatever. Why? For
> that same reason that we discourage use of functions like os.chdir or
> os.umask except in rare circumstances: the single call inevitably affects
> the entire program behaviour.

Well yes. And I would normally agree. But I think a better analogy here is
with print. By default, print outputs to stdout. You can customise that by
giving an extra argument to print:


print(spam, file=sys.stderr)  # Python 3
print >>sys.stderr, spam  # Python 2


or you can re-direct sys.stdout. Both are allowed. But doing the later is
potentially more disruptive, and should be done with care. Nevertheless, it
is supported, and Python will set up stdout to the appropriate file for you
when you start.

Although I think you're being overly cautious, I've changed the behaviour:

- the default is dunders=True, always;

- you can manually override the default with an optional, explicit dunders
keyword-only argument, always;

- you can change the default to dunders=False by setting an attribute, only
when running in interactive mode.



-- 
Steven




More information about the Python-list mailing list