How to tell which subclass was used to instantiate object

Frank Millman frank at chagford.com
Sun May 2 03:00:53 EDT 2004


"John Roth" <newsgroups at jhrothjr.com> wrote in message news:<10978dla4s8sq17 at news.supernews.com>...
> "Frank Millman" <frank at chagford.com> wrote in message
> news:246a4e07.0405010447.11ed5400 at posting.google.com...
> > Hi all
> >
> > I have a question regarding inheritance. I have come up with a
> > solution, but it is not very elegant - I am sure there is a more
> > pythonic approach. Assume the following class definitions.
> >
>  [...]
> >
> > Is there a more direct way for a top-level class to determine which
> > subclasses were used to instantiate it?
> 
> Look at the __class__ attribute. Specifically, __class__.__name__
> should tell you the name of the class.
> 
> John Roth
> >

Thanks for the reply, John. Unfortunately this does not seem to give
me what I am looking for. I can only look at the __class__ attribute
once the object has been instantiated.

I want to check the inheritance hierarchy from the top-level __init__
while the object is being instantiated, and raise an exception if it
fails my test.

It seems from Heather's reply that it is not possible to do this
directly, and therefore my inelegant solution is probably as good as
any. See my reply to Heather for further comments.

Thanks again.

Frank



More information about the Python-list mailing list