a heisenbug

Peter Otten __peter__ at web.de
Mon Aug 28 17:09:03 EDT 2017


Chris Angelico wrote:

> On Tue, Aug 29, 2017 at 4:59 AM, Stefan Ram <ram at zedat.fu-berlin.de>
> wrote:
>>   This might be what one calls "heisenbug":
>>
>>   No attribute 'abc' is visible. One tries to
>>   study it with "help". And next time it's there.
>>   "help" /did/ help!
>>
>> Python 3.6.0 ...
>>
>>>>> import collections
>>
>>>>> isinstance( 'abc', collections.abc.Sequence )
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in <module>
>> AttributeError: module 'collections' has no attribute 'abc'
>>
>>>>> help(collections)
>> Help on package collections: ...
>>
>>>>> isinstance( 'abc', collections.abc.Sequence )
>> True
> 
> When you import "collections", you load up the package. If anything
> subsequently runs "import collections.abc", the module gets loaded,
> and is visible from the package. The help function does exactly that
> import. It's a bit surprising but only an issue with interactive work.
> (You won't use help() elsewhere.)

The lesson is that if you use a (sub)module you should never rely on an 
implicit import.




More information about the Python-list mailing list