typeof in python

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):

str(type(var))
About these ads

One thought on “typeof in python

  1. 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 :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s