本文介绍了在python 2.6中检查双端队列的maxlen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我不得不从 python 2.7 更改为 2.6.
我一直在使用带有 maxlen 属性的双端队列,并且一直在检查 maxlen 是什么.显然你可以在 python 2.6 中使用 maxlen,但在 2.6 双端队列中没有 maxlen 属性.
在 python 2.6 中检查双端队列的 maxlen 的最简洁方法是什么?

在 2.7 中:

from collections import dequed = 双端队列(最大长度 = 10)打印 d.maxlen

在 2.6 中可以使用 deque,并且 maxlen 可以正常工作,但是 maxlen 不是可以引用的属性.

干杯

解决方案

我将通过继承 collections.deque 来创建我自己的 deque.这并不困难.也就是说,这里是:

导入集合类双端队列(collections.deque):def __init__(self, iterable=(), maxlen=None):super(deque, self).__init__(iterable, maxlen)self._maxlen = maxlen@财产def maxlen(自我):返回 self._maxlen

这是工作中的新双端队列:

>>>d = 双端队列()>>>打印双端队列([])>>>打印 d.maxlen没有任何>>>d = 双端队列(最大长度 = 3)>>>打印双端队列([], maxlen=3)>>>打印 d.maxlen3>>>d = 双端队列(范围(5))>>>打印双端队列([0, 1, 2, 3, 4])>>>打印 d.maxlen没有任何>>>d = 双端队列(范围(5),maxlen=3)>>>打印双端队列([2, 3, 4], maxlen=3)>>>打印 d.maxlen3


I have had to change from python 2.7 to 2.6.
I've been using a deque with the maxlen property and have been checking what the maxlen is. Apparently you can use maxlen in python 2.6, but in 2.6 deques do not have a maxlen attribute.
What is the cleanest way to check what the maxlen of a deque is in python 2.6?

In 2.7:

from collections import deque
d = deque(maxlen = 10)
print d.maxlen

In 2.6 the deque can be used and the maxlen works properly, but maxlen is not an attribute that can be referred to.

Cheers

解决方案

I would create my own deque by inheriting from collections.deque. It is not difficult. Namely, here it is:

import collections

class deque(collections.deque):
    def __init__(self, iterable=(), maxlen=None):
        super(deque, self).__init__(iterable, maxlen)
        self._maxlen = maxlen
    @property
    def maxlen(self):
        return self._maxlen

and this is the new deque at work:

>>> d = deque()
>>> print d
deque([])
>>> print d.maxlen
None
>>> d = deque(maxlen=3)
>>> print d
deque([], maxlen=3)
>>> print d.maxlen
3
>>> d = deque(range(5))
>>> print d
deque([0, 1, 2, 3, 4])
>>> print d.maxlen
None
>>> d = deque(range(5), maxlen=3)
>>> print d
deque([2, 3, 4], maxlen=3)
>>> print d.maxlen
3

这篇关于在python 2.6中检查双端队列的maxlen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 06:50