Extending the dict class

Bruno Desthuilliers onurb at xiludom.gro
Tue Aug 29 11:10:32 EDT 2006


chosechu wrote:
> Duncan Booth wrote:
>> No, you weren't able to extend the builtin dict class nor touch any its
>> constructor.
> 
> Yes, sorry. Forgot the negation.
> 
>> All you did was to create a subclass with its own constructor and hide the
>> name for the builtin dictionary type. The original type was still unchanged
>> as you can see since anything which constructed a dictionary without using
>> the name you had overwritten still got the original type.
>>
>> If you had looked at type(dict()) and type({}) after your subclassing, you
>> would see that they are different types.
> 
> ... which prompted my question.
> 
> And prompts yet another one: seems like it
> is not possible with Python to modify behaviour
> for base classes without recompiling the
> interpreter. Forgive me for asking what must
> surely have been asked already, but are there
> plans to implement something like that,
> <teasing>like Ruby</teasing>?
>
> I would not feel too safe navigating in a source
> where base object behaviour might have been
> re-defined, but it sure is a powerful way of
> adding behaviour to third-party code which you
> may not have possibility to modify.

It's usually possible to modify third-parts classes behaviour on the fly
(googling for 'monkey-patching' should get you started). But true, this
doesn't work with builtins.

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list