If a class Child inherits from Parent, how to implement Child.some_method if Parent.some_method() returns Parent instance ?

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Oct 30 06:31:16 EDT 2009


metal a écrit :
> On 10月30日, 下午4时44分, Bruno Desthuilliers <bruno.
> 42.desthuilli... at websiteburo.invalid> wrote:
>> metal a écrit :
(snip)
>>>     def methods(cls):
>>>         return [k for k, v in cls.__dict__.items() if callable(v)]
>> All callables are not functions or methods... The inspect module might
>> help you here.
>>
>>>     def wrap_methods(cls):
>>>         for k in cls.methods():
>>>             f = cls.__dict__[k]
>> Just for the record, you wouldn't have to do this lookup if .methods
>> returned the actual objects instead of their names) 

> 
> Sure I know .methods() can return actual objects if ONLY iteration,
> but how to overwrite it?

Ahem... <cough> yes, right, you do need the key too. But you still can 
avoid the extra dict lookup : just return both the key and object !-)

> type(self) in new-style class looks a little better, thanks for the
> idea

FWIW and as a general rule, __special_names__ are mostly implementation 
support for operator or operator-like generic functions, and not 
intented for direct access (except when you really need it of course).



More information about the Python-list mailing list