Get fully qualified class name of an object in Python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

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 x.__class__.__name__, but is there a simple method to get the package and module?

Answers

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
foo.Bar

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/2020014/get-fully-qualified-class-name-of-an-object-in-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils