introspection inquiry

Michael Hoffman cam.ac.uk at mh391.invalid
Sun Feb 20 11:02:20 EST 2005


Diez B. Roggisch wrote:
 >[Michael Hoffman]:
>>Unless I misunderstood the question, that won't work. That will
>>give you the name of the class the object is an instance is of.
>>I think he wants the name of the class the method was defined in.
> 
> Where is the difference? The method is defined in a class - and an instance
> is created from that class.
> 
> This works as expected:
> 
> class ExistentialCrisis:
>     def __init__(self, text):
>         self.spam = text
>         print 'In the constructor of the %s class' % self.__class__.__name__
> 
> 
> ExistentialCrisis("egal")

Yes, but this doesn't work if you have a subclass:

"""
class ExistentialCrisisSubclass(ExistentialCrisis):
     def __init__(self, text):
         print "New constructor"
         ExistentialCrisis.__init__(self, text)

ExistentialCrisisSubclass("whoa")
"""

gives you:

New constructor
In the constructor of the ExistentialCrisisSubclass class

But the second line is *not* in the constructor of
ExistentialCrisisSubclass, it is in the constructor of ExistentialCrisis.

while I read the original post as saying that he wanted
"ExistentialCrisis" there instead. Indeed this example

-- 
Michael Hoffman



More information about the Python-list mailing list