Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #66892

Re: Python stdlib code that looks odd

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'subject:Python': 0.06; 'odd': 0.07; 'report.': 0.07; 'subject:code': 0.07; 'correspond': 0.09; 'iterate': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'arg': 0.16; 'complained': 0.16; 'dict': 0.16; 'dictionary,': 0.16; 'iteritems': 0.16; 'occurences': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'removed,': 0.16; '(you': 0.16; 'weird': 0.16; 'wrote:': 0.18; 'items.': 0.19; 'example': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'looks': 0.24; 'source': 0.25; 'class.': 0.26; 'certain': 0.27; 'header:X -Complaints-To:1': 0.27; 'chris': 0.29; 'especially': 0.30; "i'm": 0.30; 'code': 0.31; "skip:' 10": 0.31; 'subject:that': 0.31; 'file': 0.32; 'class': 0.32; 'could': 0.34; 'objects': 0.35; 'but': 0.35; 'building': 0.35; 'really': 0.36; 'false': 0.36; 'done': 0.36; 'next': 0.36; 'should': 0.36; 'too': 0.37; 'list': 0.37; 'being': 0.38; 'checks': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'remove': 0.60; 'catch': 0.60; 'new': 0.61; 'first': 0.61; 'skip:n 10': 0.64; 'here': 0.66; 'mailbox': 0.68; 'nobody': 0.68; 'conservative': 0.84; 'presumably': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Python stdlib code that looks odd
Date Sat, 22 Feb 2014 12:54:59 +0100
Organization None
References <CAPTjJmrqwtAQEjxuUeqVFxy-yb_X-CWdtXyy+uUOE8MAkGQp-g@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bdbfea.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.7259.1393070095.18130.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1393070095 news.xs4all.nl 2938 [2001:888:2000:d::a6]:45118
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:66892

Show key headers only | View raw


Chris Angelico wrote:

> 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? 

Remember that you are looking at Python 3 code here where items() is the new 
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.

Someone mechanically removed occurences of iteritems() from the code and 
ended up being either too aggressive as the Mailbox class still has an 
iteritems() method, so mb1.update(mb2) could avoid building a list, or too 
conservative as Mailbox.iterXXX could be removed, and .XXX() turned into a 
view following the example of the dict class.

If nobody has complained until now (you get an error only for objects with 
an iteritems() but without an items() method, and those should be rare to 
non-existent) I think the path to progress is to remove the first 
alternative completely.

    def update(self, arg=None):
        """Change the messages that correspond to certain keys."""
        if hasattr(arg, 'items'):
            source = arg.items()
        else:
            source = arg
        bad_key = False
        ...

Please file a bug report.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Python stdlib code that looks odd Peter Otten <__peter__@web.de> - 2014-02-22 12:54 +0100

csiph-web