calling objects parents' __init__?

John Roth newsgroups at jhrothjr.com
Mon Sep 15 16:27:54 EDT 2003


<user at domain.invalid> wrote in message
news:e13963e818473ad69d0a49230e328b91 at news.teranews.com...
> Normally, when I inherit from a class that I have written,
> I want instances of the new class to call the __init__ of
> the old (actually, starting from the top of the tree, and
> moving on down).
>
> What is the preferred way to do this?

If you're using a new style class (one that inherits from
object,) look at the super() built in function. Be a bit
cautious with it, the usage isn't exactly obvious.
There's also an article on the method resolution order
somewhere that makes quite interesting reading.

If you're using an old style class, you have to call
them directly: superklas.__init__(self).

John Roth

>
> Thanks,
>
> Tobiah
>






More information about the Python-list mailing list