Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100467
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Peter Otten <__peter__@web.de> |
| Newsgroups | comp.lang.python |
| Subject | Re: subclassing collections.Counter |
| Date | Tue, 15 Dec 2015 17:08:15 +0100 |
| Organization | None |
| Lines | 26 |
| Message-ID | <mailman.28.1450195721.22044.python-list@python.org> (permalink) |
| References | <567036A5.6050205@gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | news.uni-berlin.de El2oxaJMABNaa7h18fmdnQ8vXAxW8RCZHoUrj97mbyYg== |
| 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.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:skip:c 10': 0.07; 'metrics': 0.09; 'modifies': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'increment': 0.16; 'key):': 0.16; 'keys.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'self[key]': 0.16; 'wrote:': 0.16; 'attribute': 0.18; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'idea': 0.28; 'values': 0.28; 'depth': 0.29; 'invoke': 0.29; 'thus,': 0.29; 'extend': 0.31; 'anyone': 0.32; 'skip:_ 10': 0.32; 'class': 0.33; 'this?': 0.34; 'could': 0.35; 'returning': 0.35; 'instead': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'received:org': 0.37; 'thought': 0.37; 'does': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'some': 0.40; 'maximum': 0.61; 'sum': 0.69; 'exceeded': 0.83; 'average': 0.93 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | p57bd8e96.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| 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> |
| Xref | csiph.com comp.lang.python:100467 |
Show key headers only | View raw
Pavlos Parissis wrote: > I need to store values for metrics and return the average for some > and the sum for the rest. Thus, I thought I could extend > collections.Counter class by returning averages for some keys. > > My class modifies the update() to increment a counter and the > __getitem__ to perform the calculation. But, I get RuntimeError: maximum > recursion depth exceeded as I access an attribute inside > __getitem__. > > Does anyone has an idea how I can achieve this? > class CounterExt(Counter): > def __getitem__(self, key): > if (self.avg_metrics is not None and key in self.avg_metrics): > return self[key] / self._counter > else: > return self[key] self[key] will call the CounterExt.__getitem__() method again. Use super().__getitem__(key) instead to invoke Counter.__getitem__().
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: subclassing collections.Counter Peter Otten <__peter__@web.de> - 2015-12-15 17:08 +0100
csiph-web