How to tell which subclass was used to instantiate object

Frank Millman frank at chagford.com
Sun May 2 03:34:49 EDT 2004


Heather Coppersmith <me at privacy.net> wrote in message news:<m2ad0suuyl.fsf at unique.phony.fqdn>...
> On 1 May 2004 05:47:20 -0700,
> frank at chagford.com (Frank Millman) wrote:
> 
> [ details of a multi-level object hierarchy, and worries of __init__
> methods bypassing it ]
> 
> > By inheriting from the incorrect class, the special methods to
> > handle a 'Master' type table have been bypassed. My question is,
> > how can Table check that objects have inherited from the correct
> > subclasses?
> 
> Python usually takes the "we're all adults here" point of view,
> and leaves that question to unit tests and/or code reviews.  No
> amount of B&D is sufficient to protect from a malicious coder
> anyway.
> 

I fully agree, but this is not the scenario I am trying to cater for.
I will explain what I am doing - maybe you can suggest a better
approach.

I am writing a general-purpose accounting/business package. It is the
nature of such beasts that no matter how powerful/generalised it is,
certain users will always have special requirements. I am therefore
including a large number of 'hooks' where users or their consultants
can add their own code without interfering with the main body of code.
One example of this is adding features to a particular database table
definition.

I have a standard class definition to represent a table, with various
attributes and methods. I allow the user to create their own subclass
for a particular table, adding/overriding where necessary. I have a
function to 'open' a table. This uses a try/except to 'try' to import
and instantiate a subclass for the table, 'except' instantiate the
standard class.

I have recently introduced the concept of a 'table type', such as
'Master' or 'Transaction', and have written my own subclasses with
standard methods to handle each type of table. The table type is
passed as an argument to the 'open' function. which now checks for a
user-defined subclass first, if not found checks the type to see if a
standard subclass exists, if not found instantiates the main class.
This works well.

The concern is that a table may be of type Master, but a user may
create their own subclass and inherit from Table instead of Master by
mistake. I want to detect this error and raise an exception.

> 
> You could unwind the stack frame and look at who's calling
> Table.__init__, but that's rather un-Pythonic, too.
> 
I do not know what this means, and I really do not want to know :-)

> IMO, the most Pythonic solution is to provide factory functions
> that do the Right Thing instead of instantiating your classes
> directly from your application code.
> 

If you can give a simple example, I would be interested in learning
more about this. Alternatively, I will leave things as they are for
now, and focus on getting my app to a point where I can get some live
installations. If things look promising, I will release it as Open
Source, and then I will be very happy to get feedback on this and, I
am sure, many other aspects of my coding that could be substantially
improved.

> HTH,
> Heather

Many thanks for your input.

Frank

ps What does B&D mean?



More information about the Python-list mailing list