[Python-ideas] shutil.symlink - "avoid constant bool flags"

Tom Hale tom at hale.ee
Thu May 16 10:04:29 EDT 2019


On 14/5/19 12:38 am, Steven D'Aprano wrote:
> On Mon, May 13, 2019 at 12:31:08PM +0200, Anders Hovmöller wrote:
> 
>> An optional "overwrite_if_exists=False" flag seems much nicer.
> 
> Aside from the argument name being too verbose, that violates the rule
> of thumb "avoid constant bool flags" design principle.
> 
> (Note that this is a *rule of thumb*, not a hard law: there are times
> that it can and should be violated; its also not a well-known principle
> and so lots of APIs violate it even when they shouldn't.)

Interestingly, reviewing https://docs.python.org/3/library/os.html, I 
see *many* optional arguments with a Boolean default value.

In fact, both os.link and os.symlink have one.

os.link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)

os.symlink(src, dst, target_is_directory=False, *, dir_fd=None)

The title of the page https://docs.python.org/library/os.html is:

     Miscellaneous operating system interfaces

It is interesting that a module which provides interfaces seems to so 
blatantly disregard the "avoid constant bool flags" function interface 
design principle. Given that they are interfaces, they could have been 
written in a more pythonesque way.

If the POSIX utility ln were to be implemented, there are a few binary 
options:

1. Hard or soft link
2. Overwrite or raise an error
3. Follow symlinks or link to them

If there were a different function for each, that would be a total of 8 
functions, with ugly names like "hardlink_overwrite_follow_links" or 
"softlink_non_overwrite_link_to_links".

It seems far more practicable to have only two functions with sensible 
boolean defaults, with the split being based on the underlying os module 
function, namely os.link and os.symlink.

-- 
Tom Hale


More information about the Python-ideas mailing list