the annoying, verbose self

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Tue Nov 27 05:22:35 EST 2007


MonkeeSage a écrit :
> On Nov 27, 3:20 am, Roy Smith <r... at panix.com> wrote:
> 
>> If you want to have a little fun:
>>
>> class peverse:
>>     def __call__(self):
>>         raise AttributeError ("peverse instance has no __call__ method")
>>
>> x = peverse()
>> x()

print callable(x)
=> True

> That is "peverse", but still...
> 
> from types import FunctionType
> type(x) == FunctionType # False
> 
> And you can't (easily?) subclass FunctionType:
> 
> Error when calling the metaclass bases
>   type 'function' is not an acceptable base type
> 
> ;)

You don't have to subclass function to define a callable type that 
implements the descriptor protocol so it behaves just like a function in 
the context of an attribute lookup.



More information about the Python-list mailing list