2

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?

2
  • 4
    What do you mean? From an instance, it's instance.__class__.__name__. Commented Aug 15, 2014 at 19:24
  • drop into the python prompt, instantiate your object as instance, and do dir(instance). Commented Aug 15, 2014 at 19:24

3 Answers 3

3

Example:

class MyClass: def whoami(self): return self.__class__.__name__ instance = MyClass() print instance.whoami() #<-- prints: MyClass 
Sign up to request clarification or add additional context in comments.

Comments

2

If your have an instance of the class, you can do:

return obj.__class__.__name__ 

Comments

2

You can use __class__.__name__ in your object;

print yourObject.__class__.__name__ 

Or this it not object its class, your can use this too;

class myClass: # Create class pass print myClass.__name__ # It will have 'myClass' 

Regards,

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.