0

Edited, original question below.
As pointed out by @MiH this question is nearly identical to Get subclass name?


Is there a way to get the name of the actual/run-time class of an object rather than the name of the class where a method is defined?

I call a base class method on derived class objects (such objects do not override the method), o.__class__.__name__ gives me the name of the class of the base class, not the name of the run-time class.

Obviously I want to avoid defining a method in each derived class just to get its class name, the objective of the question is to know if there is a way to avoid that.

  • 1
    I think it's a duplicate of this question: https://stackoverflow.com/questions/3314627/get-subclass-name – MiH Feb 21 '18 at 14:31
  • 1
    I'm not sure I'm following... where are methods coming into this? If you've got an object `o` - then presumably that's the object you want the type of and that's it? – Jon Clements Feb 21 '18 at 14:34
  • So you're saying `o` is of type `DerivedClass`, yet `o.__class__.__name__` is `BaseClass`? I don't think that's possible. Show us some code. – John Gordon Feb 21 '18 at 14:35
  • @MiH you are right, de facto it is a duplicate –  Feb 21 '18 at 14:45

1 Answers1

1

The complete answer to my question is

1) key thing: answer indicated by @MiH: Get subclass name?, chosen answer starting "Subclassing from object gives you new-style classes..." ,
other answers are the usual __class__.__name__ that is not right for this and that question

2) trivial pedantic bit: add .__name__ , ie. type(self).__name__