
一,__len__()方法返回长度
1,len()函数
len()函数:
功能:len() 函数返回对象(字符、列表、元组等)长度或项目个数
语法:
len( s )
参数:s : 要查询长度的对象
返回值: 返回对象长度
2,没有定义__len__()方法时,对实例应用len()函数会引发TypeError
class Student:
def __init__(self, name, age):
self.name = name
self.age = 3
tom = Student('Tom', 3)
print(len(tom))
运行结果:
Traceback (most recent call last):
File "/Users/liuhongdi/python_work/tutorial/demo1/oop/info.py", line 126, in <module>
print(len(tom))
^^^^^^^^
TypeError: object of type 'Student' has no len()
增加__len__()方法后,可以对实例应用len()函数







![给定长度为n(n<=20)的数组a,-20<=ai<=20, 每次操作选择i,j,使a[i] += a[j], 在31次操作内使a不递减,输出每次操作的i,j](https://img-blog.csdnimg.cn/direct/b7958c1f23bb46e79983ccc4e11940f6.png)






![[HTML]Web前端开发技术28(HTML5、CSS3、JavaScript )JavaScript基础——喵喵画网页](https://img-blog.csdnimg.cn/direct/1b9dece61d314044a355d452d141db77.png)




