[英]attribute 'maxlen' of 'collections.deque' objects is not writable
class TailDeque(collections.deque):
'''Implementation of deque with limited maxlen support for Python 2.5.'''
def __init__(self, iterable=None, maxlen=-1):
super(TailDeque, self).__init__([])
self.maxlen = maxlen
if iterable is not None:
self.extend(iterable)
def extend(self, iterable):
for item in iterable:
self.append(item)
def extendleft(self, iterable):
for item in iterable:
self.appendleft(item)
def appendleft(self, item):
if len(self) == self.maxlen:
self.pop()
super(TailDeque, self).appendleft(item)
def append(self, item):
if len(self) == self.maxlen:
self.popleft()
super(TailDeque, self).append(item)
logQueue = TailDeque(maxlen=20)
Can someone might explain and show me how to do this for python 3.4? 有人可以解释一下,告诉我如何为python 3.4做到这一点吗?
Im always getting 我一直都在
AttributeError: attribute 'maxlen' of 'collections.deque' objects is not writable
https://docs.python.org/2/library/collections.html#deque-objects https://docs.python.org/2/library/collections.html#deque-objects
The maxlen parameter was added to collections.deque in 2.6 and 3.0. 将maxlen参数添加到2.6和3.0中的collections.deque中。 (The read-only
maxlen
attribute, set from the argument, was added in 2.7 and 3.1.) The code you posted is intended for 2.5 as a partial substitute. (从参数设置的只读
maxlen
属性已在2.7和3.1中添加。)您发布的代码旨在将2.5用作部分替换。 For 2.6+ and 3.1+ you should just use collections.deque and not bother with the partial backport. 对于2.6+和3.1+,您应该只使用collections.deque而不用担心部分反向移植。 If you are writing code to support 2.5 to 2.7, you should replace collections.deque with its wrapper when running on 2.5.
如果要编写代码以支持2.5到2.7,则在2.5上运行时,应使用其包装器替换collections.deque。 Something like
就像是
import sys
major = sys.version[0:3]
if major >= '2.6':
from collections import deque
elif major == '2.5':
from myutils import TailDeque as deque
else:
raise MyException('This app requires 2.5 or later.')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.