OrderedCounter返回递归

2024-09-28 17:20:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我根据雷蒙德·赫廷格在PyCon 2015中的讲话,试了一下有序计数器。我正在尝试对python3.6.8执行同样的操作

from collections import Counter, OrderedDict

class OrderedCounter(Counter, OrderedDict):

    def __repr__(self):
        return "{}({})".format(self.__class__.__name__,
                               OrderedCounter(self))

    def __reduce__(self):
        return self.__class__, (OrderedCounter(self),)

尝试这个代码

ox = OrderedCounter("abrac")
ox

回溯如下

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tmp/py30539GwP", line 8, in __repr__
  File "/tmp/py30539GwP", line 8, in __repr__
  File "/tmp/py30539GwP", line 8, in __repr__
  [Previous line repeated 194 more times]
  File "/home/blue/.pyenv/versions/3.6.8/lib/python3.6/collections/__init__.py", line 535, in __init__
    self.update(*args, **kwds)
  File "/home/blue/.pyenv/versions/3.6.8/lib/python3.6/collections/__init__.py", line 614, in update
    if isinstance(iterable, Mapping):
  File "/home/blue/.pyenv/versions/3.6.8/lib/python3.6/abc.py", line 184, in __instancecheck__
    if subclass in cls._abc_cache:
  File "/home/blue/.pyenv/versions/3.6.8/lib/python3.6/_weakrefset.py", line 72, in __contains__
    wr = ref(item)
RecursionError: maximum recursion depth exceeded while calling a Python object

有人能告诉我可能的原因和解决办法吗?如果我漏掉了什么,过分强调或不强调某一点,请在评论中告诉我


Tags: inpyselfpyenvhomeliblineblue