def __len__(self)
时间: 2023-10-17 12:05:26 浏览: 88
def __len__(self)是一个特殊方法,用于定义类的对象在使用内置函数len()时的行为。通过在类的定义中添加这个方法,可以使类的对象表现得像一个可获取长度的列表。在这个方法中,可以返回类对象的长度,以便len()函数可以正确地返回这个长度。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [def __len__(self)和def __getitem__(self, idx)](https://blog.csdn.net/AdamCY888/article/details/131343104)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [def __len__(self)的作用](https://blog.csdn.net/qq_42818011/article/details/123618558)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文