Clean Singleton Docstrings

Peter Otten __peter__ at web.de
Wed Jul 13 19:54:18 EDT 2016


Lawrence D’Oliveiro wrote:

> On Friday, July 8, 2016 at 7:38:56 PM UTC+12, Peter Otten wrote:
> 
>> There is a test
>> 
>> if not object:
>>     raise ImportError('no Python documentation found for %r' % thing)
>> 
>> in the pydoc module. So all you need is to ensure that your Registry
>> evaluates to True in a boolean context, e. g. by putting something into
>> it:
> 
> And there are still those who think that Python’s lax acceptance of
> non-boolean values as booleans is a good idea...

I don't think this particular problem serves as an argument for stricter 
handling of boolean expressions because the fix

if object is not None: ...

is not completely correct, either:

$ cat demo.py
no = False
yes = True
maybe = None
$ pydoc3.4 demo.yes | head -n3
Help on bool in demo object:

demo.yes = class bool(int)
$ pydoc3.4 demo.no | head -n3
no Python documentation found for 'demo.no'

$ pydoc3.5 demo.no | head -n3
Help on bool in demo object:

demo.no = class bool(int)
$ pydoc3.5 demo.maybe | head -n3
No Python documentation found for 'demo.maybe'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.

Better would be to let the locate() function raise an exception along these 
lines:

       for part in parts[n:]:
        try:
            object = getattr(object, part)
        except AttributeError:
            # return None
            raise ImportError("Name ... not found in module ...")

PS: Do the new hints
"""
Use help() to get the interactive help utility.
Use help(str) for help on the str class.
"""
make any sense? Not to me, at this time of the day...




More information about the Python-list mailing list