English 中文(简体)
清单(y) 行为是“重”第一电话
原标题:list(y) behavior is "wrong" on first call

我有一个配有<代码>_len__方法的索引。 问题:

如果您的电话清单(y)和 y有 len__的界定方法,则请上len__

(1) 为什么?

在我的产出中,你将看到,从(y)清单来看,第一次尝试的时间是零。 如果看一看清单的产出,你就会看到,在第一次发言中,我收到一份空名单,在第二次名单上,我收到“正确”名单。

2) 为什么它回到一个零长的清单?

3) 为什么清单篇幅太长,不能满足随后的所有要求?

还指出,“数字”不是问题。 C类是相同的,但使用一种手法,然后打电话到()。

法典:

showcalls = False

class A(object):
    _length = None
    def __iter__(self):
        if showcalls:
            print "iter"
        self.i = 0
        return self        
    def next(self):
        if showcalls:
            print "next"
        i = self.i + 1
        self.i = i
        if i > 2:
            raise StopIteration
        else:
            return i

class B(A):
    def __len__(self):
        if showcalls:
            print "len"
        if self._length is None:
            for i,x in enumerate(self):
                pass
            self._length = i
            return i
        else:
            return self._length

class C(A):
    def __len__(self):
        if showcalls:
            print "len"
        if self._length is None:
            i = 0
            while True:
                try:
                    self.next()
                except StopIteration:
                    self._length = i
                    return i
                else:
                    i += 1
        else:
            return self._length

if __name__ ==  __main__ :
    a = A()
    print len(list(a)), len(list(a)), len(list(a))
    print
    b = B()
    print len(list(b)), len(list(b)), len(list(b))
    print
    c = C()
    print len(list(c)), len(list(c)), len(list(c))

产出:

2 2 2

0 2 2

0 2 2
最佳回答

If you call list(y) and y has a len method defined, then len is called. why?

因为如果从一开始就知道最后很长的时间,就能够更快地编制由此产生的清单,而不是从空名单上开始,并随时间推展一个项目。 并且必须保证100%的可靠性。

IOW, do not Implement special methods such as len__, if and when You can t 回去 a key Value.

关于第二个问题,你执行<代码> len__<>/code>是因为其consume/strong>(并且不将其退回到其原始状态)——所以没有将任何项目留给<代码>。 电话:list 施工人取得<代码>StopIteration,并决定: 只是微弱的(不幸的是,其面粉比低的<代码>>>>> > >。 ).!

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...