How can i call array_length to get the length of array object?

gmspro gmspro at yahoo.com
Sun Jun 24 10:19:20 CEST 2012


@Ian,

The function name in arraymodule.c file is array_len but you have to call something like: , why?

>>>arr=array('i',[5,8,7])
>>>lg=arr.__len__()
>>>print(lg)

Why __len__() where the original name if array_length? Why is method named like __NAME__ ?

--- On Sun, 6/24/12, gmspro <gmspro at yahoo.com> wrote:

From: gmspro <gmspro at yahoo.com>
Subject: Re: How can i call array_length to get the length of array object?
To: "Ian Kelly" <ian.g.kelly at gmail.com>
Cc: "python-list" <python-list at python.org>
Date: Sunday, June 24, 2012, 3:01 AM

Why are some methods/functions named in this way in python? __len__

underscoreunderscoreNAMEunderscoreunderscore

Is there any speciality of naming such methods?

--- On Sun, 6/24/12, Ian Kelly <ian.g.kelly at gmail.com> wrote:

From: Ian Kelly <ian.g.kelly at gmail.com>
Subject: Re: How can i call array_length to get the length of array object?
To: "gmspro" <gmspro at yahoo.com>
Cc: "python-list" <python-list at python.org>
Date: Sunday, June 24, 2012, 12:34 AM

On Sat, Jun 23, 2012 at 8:23 PM, gmspro <gmspro at yahoo.com> wrote:
> I'm trying to call this function, http://hg.python.org/cpython/file/3b7230997425/Modules/arraymodule.c#l657
>
> Is that possible to call that function?
>
> I know it's possible to do:
> >>>len(arr)

You call it just like that.  array_length is the C implementation of
__len__ for arrays.

> Doesn't it call this :
> http://hg.python.org/cpython/file/c0eab397f098/Python/bltinmodule.c#l1283
> instead of this:
> http://hg.python.org/cpython/file/3b7230997425/Modules/arraymodule.c#l657

Yes, and builtin_len calls PyObject_Size, which in turn calls the
object's sq_length method, which is defined to be
 array_length for
arrays.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120624/deb807bc/attachment.html>


More information about the Python-list mailing list