Python stdlib code that looks odd

C

Chris Angelico

I'm poking around in the stdlib, and Lib/mailbox.py has the following
inside class Mailbox:

def update(self, arg=None):
"""Change the messages that correspond to certain keys."""
if hasattr(arg, 'iteritems'):
source = arg.items()
elif hasattr(arg, 'items'):
source = arg.items()
else:
source = arg
bad_key = False
for key, message in source:
... use key/message ...

Looks odd to check if it has iteritems and then use items. Presumably
this will catch a Python 2 dictionary, and take its items as a list
rather than an iterator; but since the only thing it does with source
is iterate over it, would it be better done as iteritems? Mainly, it
just looks really weird to check for one thing and then call on
another, especially as it then checks for the other thing in the next
line.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top