I could not find a good way to get the name of a class from an instance with Python on google. It’s simple (but not obvious):
This entry was posted on February 18, 2011, 8:28 pm and is filed under Python, Sample Code. You can follow any responses to this entry through RSS 2.0.
You can leave a response, or trackback from your own site.
#1 by James Skinner on July 5, 2012 - 5:57 pm
var.__class__ will get you first the class var is an instance of.
var.__class__.__name__ will be the class’s name, var.__class__.__module__ it’s module – you can use __name__ on that as well.
There are some methods in the inspect module for doing things like getting the inheritance tree for a class if you really need it
Fill in your details below or click an icon to log in:
You are commenting using your WordPress.com account. ( Log Out / Change )
You are commenting using your Twitter account. ( Log Out / Change )
You are commenting using your Facebook account. ( Log Out / Change )
You are commenting using your Google+ account. ( Log Out / Change )
Connecting to %s
Notify me of follow-up comments via email.
Blog at WordPress.com.
The Fusion Theme.
Get every new post delivered to your Inbox.