list .__ len __()或len(list) [英] list.__len__() or len(list)

查看:316
本文介绍了list .__ len __()或len(list)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪一个更好?为什么?


__len __()是列表对象的内置函数,并使用列表对象元素沿着

更新,并且有用这个列表非常好

巨大。


len()再次是外部方法,可能需要再次处理

周期。


是不是?

which one is better? and why?

__len__() is a built-in function of the list object and is updated along
with the list object elements and will be useful incase the list is very
huge.

len() is an external method again, which may require the processing
cycles again.

Is it right?

推荐答案

5月13日,6:57 * pm,Nikhil< ; mnik ... @ gmail.comwrote:
On May 13, 6:57*pm, Nikhil <mnik...@gmail.comwrote:

哪个更好?为什么?


__len __()是列表对象的内置函数,并使用列表对象元素沿着

更新,并且有用这个列表非常好

巨大。


len()再次是外部方法,可能需要再次处理

周期。


是不是?
which one is better? and why?

__len__() is a built-in function of the list object and is updated along
with the list object elements and will be useful incase the list is very
huge.

len() is an external method again, which may require the processing
cycles again.

Is it right?



len()是正确的方法,以__开头的方法是

意味着更内部。

len() is the correct way to do it, methods that begin with __ are
meant to be more internal.


2008年5月13日星期二下午5:57,Nikhil< mn ***** @ gmail.com写道:
On Tue, May 13, 2008 at 5:57 PM, Nikhil <mn*****@gmail.comwrote:

__len __()是列表对象的内置函数,并且随着列表对象元素一起更新

并且在列表非常有用的情况下非常有用

巨大。


len()再次是一种外部方法,可能需要再次处理周期


__len__() is a built-in function of the list object and is updated along
with the list object elements and will be useful incase the list is very
huge.

len() is an external method again, which may require the processing cycles
again.



obj .__ len __()的目的是实现len(obj),它简单地称为
。所以obj .__ len __()可能会更快,但只是略有增加。

更喜欢len(obj)的理由是如果你无意中传递了一个没有实现__len__的对象

,你会得到更合适的

TypeError而不是AttributeError。

The purpose of obj.__len__() is to implement len(obj), which simply
calls it. So obj.__len__() may be faster, but only marginally. The
reason to prefer len(obj) is that if you inadvertently pass an object
that does not implement __len__, you get the more appropriate
TypeError rather than an AttributeError.


Ian Kelly schrieb:
Ian Kelly schrieb:

obj .__ len __()的目的是实现len(obj),它简单地称为
。所以obj .__ len __()可能会更快,但只是略有增加。

更喜欢len(obj)的理由是如果你无意中传递了一个没有实现__len__的对象

,你会得到更合适的

TypeError而不是AttributeError。
The purpose of obj.__len__() is to implement len(obj), which simply
calls it. So obj.__len__() may be faster, but only marginally. The
reason to prefer len(obj) is that if you inadvertently pass an object
that does not implement __len__, you get the more appropriate
TypeError rather than an AttributeError.



len(obj)对于像str这样的几种类型比obj .__ len __()更快。在

中,一般len()至少与__len __()一样快。 len()还做了一些额外的

健全性检查。


python2.5 -m timeit"''abc''.__ len __()"

1000000循环,最佳3:每循环0.453 usec

python2.5 -m timeit" len(''abc'')"

1000000循环,最佳3:每循环0.292 usec


公共代码路径已经过高度优化。除非你真的明白解释器里面发生了什么,否则不要试图聪明。

。由于某种原因,

__internal__方法被称为魔术方法。 ;)


Christian

len(obj) is faster than obj.__len__() for several types like str. In
general len() is as least as fast __len__(). len() also does some extra
sanity checks.

python2.5 -m timeit "''abc''.__len__()"
1000000 loops, best of 3: 0.453 usec per loop

python2.5 -m timeit "len(''abc'')"
1000000 loops, best of 3: 0.292 usec per loop

Common code paths are already highly optimized. Don''t try to be clever
unless you really understand what happens inside the interpreter. The
__internal__ methods are called magic methods for a reason. ;)

Christian


这篇关于list .__ len __()或len(list)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆