On the problem of _ _ len__ function of python

class Echo():
    def __init__(self, name):
        self.name = name
        print("Hello {}!".format(name))
    def __len__(self):
        n=0
        name=self.name
        for i in name:
            if i.isupper()==True:
                n=n+1;
        print("Hello {}!".format(n))
s = input()
echoA = Echo(s)
len(echoA)

I would like to ask you why you reported this mistake "NoneType" object cannot be interpreted as an integer

.
Aug.11,2021

when defining the magic method def _ _ len__ (self) , you need to return a value, that is, _ _ len__ () should return > = 0 . No, no, no. The return statement is not shown here, so None


is returned by default.

python.org/3/reference/datamodel.html?highlight=__len__-sharpobject.__len__" rel=" nofollow noreferrer "> _ _ len__: official document

the method must have a return value, and the return value is integer > = 0.


>>> class Echo():
...     def __init__(self, name):
...         self.name = name
...         print("Hello {}!".format(name))
...     def __len__(self):
...         n=0
...         name=self.name
...         for i in name:
...             if i.isupper()==True:
...                 n=n+1;
...         return n
...
>>> s = input()
WWW.OS373.CN
>>> echoA = Echo(s)
Hello WWW.OS373.CN!
>>> len(echoA)
7
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3a314-41235.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3a314-41235.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?