Le 30/11/2010 23:08, Masklinn a écrit :
On 2010-11-30, at 20:14 , Éric Araujo wrote:
For instance, as of Python 2.7 these blocks of code are roughly equivalent (the first two are in fact identical in effect):
if isinstance(obj, collections.Sized): doSomethingWith(len(obj))
if hasattr(obj, '__len__'): doSomethingWith(len(obj))
try: doSomethingWith(len(obj)) except TypeError: pass
They all test for the same thing: that you can call len() on the object.
Not exactly: magic methods are looked up on the class, not on the instance. So? So it’s possible that an object has a __len__ attribute but that calling len on it raises a TypeError. See also Nick’s reply.
May I know why you're replying that to *my* mail? Because I quoted a bit of your message to reply to it. (I’m not sure I understand the question.)
Regards