Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80283
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; 'python,': 0.02; 'affected': 0.07; 'attribute': 0.07; 'incompatible': 0.07; '[1,': 0.09; '__init__': 0.09; 'handful': 0.09; 'operand': 0.09; 'pretend': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'style.': 0.09; 'way:': 0.09; 'worse': 0.09; 'python': 0.11; '**kwds)': 0.16; 'b=2)': 0.16; 'behave': 0.16; 'collections': 0.16; 'counter()': 0.16; 'debugging,': 0.16; 'in-place': 0.16; 'instead:': 0.16; 'iterable': 0.16; 'iterable)': 0.16; 'iterable,': 0.16; 'maintainer': 0.16; 'oddity': 0.16; 'operators.': 0.16; 'positional': 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; 'typeerror:': 0.16; "{'a':": 0.16; 'all.': 0.16; 'fix': 0.17; 'wrote:': 0.18; 'module': 0.19; 'not,': 0.20; '(the': 0.22; '>>>': 0.22; 'import': 0.22; '(in': 0.22; 'tests': 0.22; 'header:User- Agent:1': 0.23; 'error': 0.23; 'skip:" 40': 0.26; 'skip:_ 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'specifically': 0.29; 'thus': 0.29; "i'm": 0.30; 'code': 0.31; 'you?': 0.31; '"",': 0.31; 'minor': 0.31; 'raised': 0.31; 'anyone': 0.31; 'file': 0.32; 'quite': 0.32; '(most': 0.33; 'classes': 0.35; 'anybody': 0.35; 'but': 0.35; 'there': 0.35; 'in:': 0.36; 'sequence': 0.36; 'up!': 0.36; 'so,': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'expect': 0.39; 'bad': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'greatest': 0.60; 'most': 0.60; 'break': 0.61; 'first': 0.61; 'places': 0.64; 'background:': 0.68; 'real-world': 0.68; 'bitten': 0.84; 'ethan': 0.84; 'furman': 0.84; 'more:': 0.84; 'type(s)': 0.84; 'gift': 0.85; 'hate': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Real-world use of Counter |
| Date | Wed, 05 Nov 2014 18:34:40 +0100 |
| Organization | None |
| References | <545A4FCC.4090503@stoneleaf.us> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd843b.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.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.15508.1415208898.18130.python-list@python.org> (permalink) |
| Lines | 82 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1415208898 news.xs4all.nl 2912 [2001:888:2000:d::a6]:58377 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:80283 |
Show key headers only | View raw
Ethan Furman wrote:
> I'm looking for real-world uses of collections.Counter, specifically to
> see if anyone has been surprised by, or had to spend extra-time debugging,
> issues with the in-place operators.
>
> Background:
>
> Most Python data types will cause a TypeError to be raised if unusable
> types are passed in:
>
> --> {'a': 0}.update(5)
> TypeError: 'int' object is not iterable
>
> --> [1, 2, 3].extend(3.14)
> TypeError: 'float' object is not iterable
>
> --> from collections import Counter
> --> Counter() + [1, 2, 3]
> TypeError: unsupported operand type(s) for +: 'Counter' and 'list'
>
> Most Counter in-place methods also behave this way:
>
> --> c /= [1, 2, 3]
> TypeError: unsupported operand type(s) for /=: 'Counter' and 'list'
>
> However, in the case of a handful of Counter in-place methods (+=, -=, &=,
> and |=), this is what happens instead:
>
> --> c = Counter()
> --> c += [1, 2, 3]
> AttributeError: 'list' object has no attribute 'items'
>
> Even worse (in my opinion) is the case of an empty Counter `and`ed with an
> incompatible type:
>
> --> c &= [1, 2, 3]
> -->
>
> No error is raised at all.
>
> In order to avoid unnecessary code churn (the fix itself is quite simple),
> the maintainer of the collections module wants to know if anybody has
> actually been affected by these inconsistencies, and if so, whether it was
> a minor inconvenience, or a compelling use-case.
>
> So, if this has bitten you, now is the time to speak up! :)
Some more:
>>> Counter(a=1, b=2)
Counter({'b': 2, 'a': 1})
>>> Counter(a=1, b=2, iterable=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/collections/__init__.py", line 462, in __init__
self.update(iterable, **kwds)
File "/usr/lib/python3.4/collections/__init__.py", line 542, in update
_count_elements(self, iterable)
TypeError: 'int' object is not iterable
This is just the first oddity I came up with. I expect that the
possibilities to break Python-coded classes are unlimited, as a result of
the -- for me -- greatest gift of Python, namely: duck-typing. I hate the
sum() implementation, not because I ever plan to "add" strings, but because
it tests against bad style. Consenting adults, where are you?
In particular for the &= fix, would
>>> c = Counter([1, 2])
>>> c &= [1, 2, 3]
>>> c
Counter({1: 1, 2: 1})
still be allowed? If not, are there other places where a sequence might
pretend to be a mapping?
FTR, as I'm not exactly a power-user of Counter, I think I have used only
the initializer with one positional iterable, and most_common(), and thus
never been bitten by any of the above pitfalls.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Real-world use of Counter Peter Otten <__peter__@web.de> - 2014-11-05 18:34 +0100
csiph-web