Hanno S. Hanno S. - 1 year ago 172
Python Question

Get fully qualified class name of an object in Python

For logging purposes I want to retrieve the fully qualified class name of a Python object. (With fully qualified I mean the class name including the package and module name.)

I know about

, but is there a simple method to get the package and module?

Answer Source

With the following program

#! /usr/bin/env python

import foo

def fullname(o):
  return o.__module__ + "." + o.__class__.__name__

bar = foo.Bar()
print fullname(bar)

and Bar defined as

class Bar(object):
  def __init__(self, v=42):
    self.val = v

the output is

$ ./prog.py
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download