Is there any case where len(someObj) does not call someObj's __len__ function?

前端 未结 5 1489
挽巷
挽巷 2020-12-06 02:45

Is there any case where len(someObj) does not call someObj\'s __len__ function?

I recently replaced the former with the latter in a (sucess

5条回答
  •  星月不相逢
    2020-12-06 03:14

    I think the answer is that it will always work -- according to the Python docs:

    __len__(self):
    

    Called to implement the built-in function len(). Should return the length of the object, an integer >= 0. Also, an object that doesn't define a __nonzero__() method and whose __len__() method returns zero is considered to be false in a Boolean context.

提交回复
热议问题