[New-bugs-announce] [issue20729] mailbox.Mailbox does odd hasattr() check

Chris Angelico report at bugs.python.org
Sat Feb 22 13:45:39 CET 2014


New submission from Chris Angelico:

Only noticed because I was searching the stdlib for hasattr calls, but in mailbox.Mailbox.update(), a check is done thus:

        if hasattr(arg, 'iteritems'):
            source = arg.items()
        elif hasattr(arg, 'items'):
            source = arg.items()
        else:
            source = arg

If this is meant to support Python 2, it should probably use iteritems() in the first branch, but for Python 3, it's probably simpler to just drop the first check altogether:

        if hasattr(arg, 'items'):
            source = arg.items()
        else:
            source = arg

Or possibly switch to EAFP:

        try:
            source = arg.items()
        except AttributeError:
            source = arg

----------
messages: 211920
nosy: Rosuav
priority: normal
severity: normal
status: open
title: mailbox.Mailbox does odd hasattr() check

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue20729>
_______________________________________


More information about the New-bugs-announce mailing list