Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have one function in my script where in the end I'm getting the class object. I want to return a class name from that function. What is the way to get classname from the respective class object?
instance.__class__.__name__
instance
dir(instance)
Example:
class MyClass: def whoami(self): return self.__class__.__name__ instance = MyClass() print instance.whoami() #<-- prints: MyClass
Add a comment
If your have an instance of the class, you can do:
return obj.__class__.__name__
You can use __class__.__name__ in your object;
__class__.__name__
print yourObject.__class__.__name__
Or this it not object its class, your can use this too;
class
class myClass: # Create class pass print myClass.__name__ # It will have 'myClass'
Regards,
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
instance.__class__.__name__.instance, and dodir(instance).