当类的形式类似于列表时,需要使用len()函数,但这时必须要提供一个特殊的方法__len__(),来返回元素个数.
class Student(object):
def __init__(self,*arg):
self.name = arg
def __len__(object):
return len(self.names)
SS= Student('Bob', 'Alice', 'Tim')
print(len(SS))
#####
3
当类的形式类似于列表时,需要使用len()函数,但这时必须要提供一个特殊的方法__len__(),来返回元素个数.
class Student(object):
def __init__(self,*arg):
self.name = arg
def __len__(object):
return len(self.names)
SS= Student('Bob', 'Alice', 'Tim')
print(len(SS))
#####
3