Magical method __len__()

Question:

How to call the __len__() function using an object of the class ?

class foo(object):
      def __init__(self,data)
         self.data = data
      def __len__(self):
         return len(self.data)
x = foo([1,2,3,4])
Asked By: VIMALAN S S

||

Answers:

You can do it this way:

>>>x = foo([1,2,3,4])
>>>len(x)
4
Answered By: Tugay

Same way you call any other function. By its name.

print(x.__len__())

which will give 4 for your code

Answered By: lllrnr101

The idea behind a magic method is to be able to call it as x.__len__() or len(x). They don’t return the output until explicitly called or have or stored in class variables.

Method 1: Call function explicitly

You can simply call the function explicitly as –

class foo(object):
    def __init__(self,data):
        self.data = data
    def __len__(self):
        print('i am in a magic function')
        return len(self.data)

x = foo([1,2,3,4])
len(x)    #or x.__len__() which is equivalent
i am in a magic function
4

Method 2: Display during initialization

Or if you want to trigger it during initialization, just add it in the __init__(). Remember, init wouldn’t return anything so you can push the output into stdio with a print.

class foo(object):
    def __init__(self,data):
        self.data = data
        print(self.__len__())
    def __len__(self):
        print('i am in a magic function')
        return len(self.data)

x = foo([1,2,3,4])
i am in a magic function
4

Method 3: Store and access as a class variable

If you want to save it, then you can define a self.length variable which can store it and can be retrieved by x.length

class foo(object):
    def __init__(self,data):
        self.data = data
        self.length = self.__len__()
    def __len__(self):
        return len(self.data)

x = foo([1,2,3,4])
x.length
4
Answered By: Akshay Sehgal

If we go with your class called foo() we can call the method __len__ like this.

a =  foo([1,2,3,4])
b = a.__len__()

Or if you want to save the length within the class:

class foo(object):
    def __init__(self,data)
        self.data = data
        self.len = None
    def __len__(self):
        self.len = len(self.data)     

a = foo([1,2,3,4])
a.__len__()

print(a.len)
Answered By: theastronomist

It basically allows you to use len().

Imagine you have only:

class foo(object):
    def __init__(self,data):
        self.data = data

x = foo([1,2,3,4])
print(len(x))

enter image description here


Now, if you have:

class foo(object):
    def __init__(self,data):
        self.data = data
        self.length = len(data)

x = foo([1,2,3,4])
print(len(x))

You still have the error:
enter image description here

(You can get the length with x.length though)


But if you add the magical method __len__():

class foo(object):
    def __init__(self,data):
        self.data = data
        self.length = len(data)

    def __len__(self):
        return self.length

x = foo([1,2,3,4])
print(len(x))

You now use len() successfully.

enter image description here

Answered By: Ka Wa Yip