Confused about class behavior

Erik Max Francis max at alcyone.com
Sat Sep 16 15:38:46 EDT 2000


Brett Lempereur wrote:

> anyway, you mentioned how to get it to inherit, but how about stuff
> like
> virtual and friend C++ functions, can you do that in Python too?

Virtual member functions are handled in Python by simply overriding the
behavior you want.

    class Base:
        def func(self):
            ... base class behavior ...

To override the method, simply redefine it in the derived class:

    class Derived(Base):
        def func(self):
            ... overridden behavior ...

Unlike C++, Python does not have access restrictions, so there is no use
for a `friend' function (everything can access everything else's
attributes and methods).  Object orientation in Python is based on the
presumption that everybody's going to behave like they should, rather
than in other languages with access restriction such as C++, where it's
presumed that the API programmer can't be trusted.  For Python that
makes more sense, as Python is commonly used as a rapid application
development language, where such access restriction wouldn't be
exploited very much.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Once the people begin to reason, all is lost.
\__/ Voltaire
    Physics reference / http://www.alcyone.com/max/reference/physics/
 A physics reference.



More information about the Python-list mailing list