How To/python/

@sequence.length()@

There is no such method for sequences in python. So you are not able to call @list.length()@, @dict.length()@, @tuple.length()@ etc.

The official explanation is that it is more readable but i don't believe that. If you are creating your own Classes you can add lenght() method and still have builtin @len()@ support.

builtin len() function

To get length of a sequence use builtin len() function:

len([0,1,2,3,4])
# returns 5

Your own sequence with len() support

When you call @len()@ function it calls builtin @sequence.len()@ method. So if you want to create your own object with len() support you have to add your own @len()@ method:

class MyObject:
    def __len__(self):
        return 100
 
x = MyObject()
len(x)
# returns 100