the annoying, verbose self

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Wed Nov 28 14:22:17 CET 2007


Steven D'Aprano a écrit :
> On Tue, 27 Nov 2007 10:11:48 +0100, Bruno Desthuilliers wrote:
> 
>> Fine. Now since Python let you define your own callable types and your
>> own descriptors, you can as well have an attribute that behave just like
>> a method without being an instance of any of the method types - so the
>> above test defeats duck typing. And since you can have callable
>> attributes that are definitively not methods, you can't rely on the fact
>> that an attribute is callable neither.
> 
> I did say the usual way was to call it and see what happens :)

It's certainly *not* "the usual way" to test if some object is callable 
- it's only ok if you really intented to call it, and even then it might 
be better to test before calling so you distinguish exceptions raised 
from within the called code from exceptions due the the call itself.

> (In Python3, I understand that is what callable() will do.

IIRC, there's no callable() function at all in Python 3.

> 
> I also didn't mention classmethods, staticmethods or functions assigned 
> to attributes. As Ton van Vliet is a beginner, I didn't think he needed 
> to be flooded with too many complications all at once.

Understanding the object model of a (mostly) OO langage is not what I'd 
call "too many complications". But I may be a bit biased !-)

> It's quite 
> possible to program in Python for years and never come across a callable 
> attribute that isn't an ordinary method.

You loose !-)

callable(object.__class__)
=> True



More information about the Python-list mailing list