Name conflict in class hierarchy

Ralf Muschall rmuschall at tecont.de
Sat May 20 20:38:18 EDT 2006


Jeffrey Barish wrote:

[overriding of base class member functions by subclass]

> but then B does not inherit other functions of A that I would like to use. 
> It struck me that this must be a common problem in OOP, so I'm wondering
> whether there is a simple solution that I am missing.

In C++, one has virtual member functions (which behave like python's),
and their purpose is to be overriden (o.g. a print method, whose subclass
version will print more attributes than the base knows about).

Such functions usually are documented - otherwise their being virtual
would make no sense.

In Python, a function not intended to be overriden should be either
have a name starting with an underscore or be documented.  So the person
who should change something is the author of the library, not you.

In your current situation, the following options seem to remain:

1. Use funny names ("foobar239847562384756" instead of "func")
   and hope that the library update will not use them

2. Call dir(classname) at hacking time and avoid names listed
   therein.  This breaks if the library update may happen at the
   user's site without you being able to change your code.

3. Call dir(classname) at runtime and raise an exception
   if it contains a name used by you.

Ralf



More information about the Python-list mailing list