[Python-ideas] bool.from_config_str()

Steven D'Aprano steve at pearwood.info
Sun Jun 12 23:12:39 EDT 2016


Hi Josh, and welcome!

On Sun, Jun 12, 2016 at 07:52:28PM -0400, jab at math.brown.edu wrote:

[...]

> >>> bool.from_config_str('false')
> False
> >>> bool.from_config_str('False')
> False
> >>> bool.from_config_str('True')
> True
> >>> bool.from_config_str('true')
> True
> >>> bool.from_config_str('0')
> False
> >>> bool.from_config_str('')
> False
> >>> bool.from_config_str('1')
> True

You have certainly found a need, but tempting as it is, I don't believe 
it should be in the standard library or a builtin. While it is true that 
nearly everyone will, at some point, want to convert human-readable 
strings to bools, the details of how and what they do are too 
individual.

Some people will want to treat:

    true yes 1 on

    false no 0 off

as true and false values, while others will have other ideas:

    vérité oui si wahr верный ja да
    aus ab unwahr 

etc. The point is, we cannot assume that all Python users will expect 
"yes" to map to True, and "no" to map to False. Since config files are 
generally written in the end-user's natural language, we can't even 
expect them to want "true" to map to True.

But I do think that this is a great candidate for a utility function in 
your own personal toolbox. There's no need for it to be in the 
*standard* library when it can be in your own *personal* library.


-- 
Steve


More information about the Python-ideas mailing list