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

Asked By: Hanno S.



__module__ would do the trick.


>>> import re
>>> print re.compile.__module__

This site suggests that __package__ might work for Python 3.0; However, the examples given there won’t work under my Python 2.5.2 console.

Answered By: Adam Matan

Consider using the inspect module which has functions like getmodule which might be what are looking for:

>>>import inspect
>>>import xml.etree.ElementTree
>>>et = xml.etree.ElementTree.ElementTree()
<module 'xml.etree.ElementTree' from 
Answered By: Tendayi Mawushe

With the following program

#!/usr/bin/env python

import foo

def fullname(o):
    klass = o.__class__
    module = klass.__module__
    if module == 'builtins':
        return klass.__qualname__ # avoid outputs like 'builtins.str'
    return module + '.' + klass.__qualname__

bar = foo.Bar()

and Bar defined as

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

the output is

$ ./

If you’re still stuck on Python 2, you’ll have to use __name__ instead of __qualname__, which is less informative for nested classes – a class Bar nested in a class Foo will show up as Bar instead of Foo.Bar:

def fullname(o):
    klass = o.__class__
    module = klass.__module__
    if module == '__builtin__':
        return klass.__name__ # avoid outputs like '__builtin__.str'
    return module + '.' + klass.__name__
Answered By: Greg Bacon

Since the interest of this topic is to get fully qualified names, here is a pitfall that occurs when using relative imports along with the main module existing in the same package. E.g., with the below module setup:

$ cat /tmp/fqname/foo/
$ cat /tmp/fqname/foo/
from baz import Baz
print Baz.__module__
$ cat /tmp/fqname/foo/
class Baz: pass
$ cat /tmp/fqname/
from foo.baz import Baz
print Baz.__module__
$ cat /tmp/fqname/foo/
import bar

Here is the output showing the result of importing the same module differently:

$ export PYTHONPATH=/tmp/fqname
$ python /tmp/fqname/
$ python /tmp/fqname/foo/
$ python /tmp/fqname/foo/

When hum imports bar using relative path, bar sees Baz.__module__ as just “baz”, but in the second import that uses full name, bar sees the same as “foo.baz”.

If you are persisting the fully-qualified names somewhere, it is better to avoid relative imports for those classes.

Answered By: haridsv

Here’s one based on Greg Bacon’s excellent answer, but with a couple of extra checks:

__module__ can be None (according to the docs), and also for a type like str it can be __builtin__ (which you might not want appearing in logs or whatever). The following checks for both those possibilities:

def fullname(o):
    module = o.__class__.__module__
    if module is None or module == str.__class__.__module__:
        return o.__class__.__name__
    return module + '.' + o.__class__.__name__

(There might be a better way to check for __builtin__. The above just relies on the fact that str is always available, and its module is always __builtin__)

Answered By: MB.

The provided answers don’t deal with nested classes.

Since Python 3.3 (PEP 3155), you can use __qualname__ of the class instead of the __name__. Otherwise, a class like

class Foo:
    class Bar: # this one

will show up as just Bar instead of Foo.Bar.

(You’ll still need to attach the __module__ to the qualname separately – __qualname__ is not intended to include module names.)

Answered By: Nathan Binkert

This is a hack but I’m supporting 2.6 and just need something simple:

>>> from logging.handlers import MemoryHandler as MH
>>> str(MH).split("'")[1]

Answered By: Gringo Suave

None of the answers here worked for me. In my case, I was using Python 2.7 and knew that I would only be working with newstyle object classes.

def get_qualified_python_name_from_class(model):
    c = model.__class__.__mro__[0]
    name = c.__module__ + "." + c.__name__
    return name
Answered By: Myer

Some people (e.g. arguing that __qualname__ is better than __name__.
Here is an example that shows the difference:

$ cat 
class One:
    class Two:

$ python3.6
>>> import dummy
>>> print(dummy.One)
<class 'dummy.One'>
>>> print(dummy.One.Two)
<class 'dummy.One.Two'>
>>> def full_name_with_name(klass):
...     return f'{klass.__module__}.{klass.__name__}'
>>> def full_name_with_qualname(klass):
...     return f'{klass.__module__}.{klass.__qualname__}'
>>> print(full_name_with_name(dummy.One))  # Correct
>>> print(full_name_with_name(dummy.One.Two))  # Wrong
>>> print(full_name_with_qualname(dummy.One))  # Correct
>>> print(full_name_with_qualname(dummy.One.Two))  # Correct

Note, it also works correctly for builtins:

>>> print(full_name_with_qualname(print))
>>> import builtins
>>> builtins.print
<built-in function print>
Answered By: Christoph Boeddeker

For python3.7 I use:

".".join([obj.__module__, obj.__name__])


Answered By: Prisacari Dmitrii

This is an adaption of the answers by Greg Bacon and MB to use the qualified class name. Note that the question did ask for the qualified class name. It was tested with Python 3.8.

def fullname(obj: object) -> str:
    """Return the full name of the given object using its module and qualified class names."""
    # Ref:
    module_name, class_name = obj.__class__.__module__, obj.__class__.__qualname__
    if module_name in (None, str.__class__.__module__):
        return class_name
    return module_name + "." + class_name

Answered By: Asclepius

Bellow is just an improvement of Greg Bacon’s answer, tested for class, instance, method, function, both builtin and user defined.

def fullname(o):
        # if o is a class or function, get module directly
        module = o.__module__
    except AttributeError:
        # then get module from o's class
        module = o.__class__.__module__
        # if o is a class or function, get name directly
        name = o.__qualname__
    except AttributeError:
        # then get o's class name
        name = o.__class__.__qualname__
    # if o is a method of builtin class, then module will be None
    if module == 'builtins' or module is None:
        return name
    return module + '.' + name
Answered By: jayvynl

My solution is:

def fullname(obj) -> str:
    if type(obj).__qualname__ != "type":
        # obj is instance
        return ".".join(
    # obj is not instance
    return ".".join([obj.__module__, obj.__qualname__])
 # not instance
 >>> print(fullname(datetime))
 # instance
 >>> print(fullname(
 # instance
 >>> print(fullname(3))
Answered By: Chad Lowe
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.