On Mon, Jul 30, 2012 at 12:51 PM, Guido van Rossum email@example.com wrote:
- Most importantly: calling len(obj) and catching TypeError can only
be a substitute for the real implementation, which IMO ought to check for the presence of a tp_len slot. Alas, checking hasattr(obj, '__len__') doesn't quite cut it either, since this returns true for a class object that defines a __len__ method for its instances (the class itself doesn't have a length).
This isn't the only place this pattern comes up; maybe a hasmethod() function somewhere (builtin, operator, inspect?) for this would be a good idea. (i.e., something that returns true only if the method is for the instance.)
(But perhaps that's a python-ideas topic, since it raises the question of whether it should really be something more like instancehasattr(), or whether it should be limited to special slots or something else.)