Introspection Question: Determining subclass name in base class
Robert Brewer
fumanchu at amor.org
Mon Nov 22 18:50:47 EST 2004
Stephen Nesbitt wrote:
> Here's my implementation problem. I have a base class which has the
> responsibility for providing entry into the logging system. Part of
> the class responsibility is to ensure that lagger names are
> consistent. For all intents and purposes this class should be
> considered abstract and will always be subclassed.
>
> What I want to do is the following:
> - allow the logger name to set explicitly. I've accomplished this by
> adding a loggerName parameter to __init__ in the base class.
> - if the loggerName variable is None, then set it to the name of the
> subclass for which we are initing.
>
> There seem to be two issues here.
> 1) determining the subclass name inside the _init__method in the
> abstract base class. All my efforts so far have resulted in the
> name of the base class rather than the calling subclass.
So far, I think I understood what you're going for, but not why you're
running into problems. Here's an example of what I think does what you
want:
>>> class A(object):
... def __init__(self, loggerName=None):
... self.loggerName = loggerName or self.__class__.__name__
...
>>> A().loggerName
'A'
>>> class B(A): pass
...
>>> B().loggerName
'B'
> 2) setting the default of a parameter to a function.
Now this, I don't understand, and that makes me think I misinterpreted
all the previous statements. Show us some code to make it more clear...?
Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org
More information about the Python-list
mailing list