OOP in python

Peter Hansen peter at engcorp.com
Wed Mar 12 09:54:25 EST 2003


Bob Roberts wrote:
> 
> What exactly do you inherit from another class in python?  With no
> type checking in python, the type doesn't matter, and the __init__()
> function of the parent doesn't seem to be called (unless you
> explicitly call it yourself).  Does it inherit anything more than just
> the member functions?

The __init__ method is called if you don't override it with your
own in the subclass:

Python 2.0 (#8, Oct 16 2000, 17:27:58) [MSC 32 bit (Intel)] on win32
>>> class A:
...   def __init__(self): print 'an A'
...
>>> class B(A):
...   pass
...
>>> b = B()
an A


As to the rest of the question: you inherit _all_ attributes which
are not prefixed with a double underscore.  This means the member
functions, as you surmised, but also any other attributes.  (Member
functions are simply attributes which are callable.)  You also 
*do* inherit the "type" in that a test using isinstance() shows
the right thing:

>>> isinstance(b, A)
1

It's just not as critical that you inherit the class/type when you rely
on signature-based polymorphism instead of other approaches.

-Peter




More information about the Python-list mailing list