Get the fully qualified object class name in Python

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

I know about x.__class__.__name__ , but is there an easy way to get the package and module?

+112
python
Jan 07 '10 at 11:50
source share
10 answers

With the next program

 #! /usr/bin/env python import foo def fullname(o): # o.__module__ + "." + o.__class__.__qualname__ is an example in # this context of HL Mencken "neat, plausible, and wrong." # Python makes no guarantees as to whether the __module__ special # attribute is defined, so we take a more circumspect approach. # Alas, the module name is explicitly excluded from __qualname__ # in Python 3. module = o.__class__.__module__ if module is None or module == str.__class__.__module__: return o.__class__.__name__ # Avoid reporting __builtin__ else: return module + '.' + o.__class__.__name__ bar = foo.Bar() print fullname(bar) 

and Bar is defined as

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

exit

 $ ./prog.py foo.Bar 
+120
Jan 07 '10 at 12:03
source share
— -

The answers provided do not apply to nested classes. Although not available until Python 3.3 ( PEP 3155 ), you really want to use __qualname__ for this class. In the end (3.4? PEP 395 ), __qualname__ will also exist for modules to deal with cases where a module is renamed (i.e. when it is renamed to __main__ ).

+27
May 26 '13 at
source share

Consider using the inspect module, which has features like getmodule , which may be what it is looking for:

 >>>import inspect >>>import xml.etree.ElementTree >>>et = xml.etree.ElementTree.ElementTree() >>>inspect.getmodule(et) <module 'xml.etree.ElementTree' from 'D:\tools\python2.5.2\lib\xml\etree\ElementTree.pyc'> 
+21
Jan 07
source share

Here's one based on Greg Bacon's great answer, but with a few extra checks:

__module__ can be None (according to the docs), and also for a type like str it can be __builtin__ (which you probably don't want to appear in magazines or anything else). The following checks for both of these features:

 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__ 

(Perhaps there is a better way to check __builtin__ . The above just depends on the fact that str is always available, and its module is always __builtin__ )

+13
Nov 30 '12 at 21:17
source share

__module__ will do the trick.

Try:

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

This site suggests that __package__ may work for Python 3.0; However, the examples given here will not work under my Python 2.5.2 console.

+9
Jan 07 '10 at 11:52
source share

This is a hack, but I support 2.6 and just need something simple:

 >>> from logging.handlers import MemoryHandler as MH >>> str(MH).split("'")[1] 'logging.handlers.MemoryHandler' 
+6
Oct. 15 '13 at 18:12
source share

Since the interest of this topic is to get full names, here is the trap that arises when using relative imports along with the main module existing in the same package. For example, with the module settings below:

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

Here is the output showing the result of importing the same module in different ways:

 $ export PYTHONPATH=/tmp/fqname $ python /tmp/fqname/main.py foo.baz foo.baz $ python /tmp/fqname/foo/bar.py baz $ python /tmp/fqname/foo/hum.py baz foo.baz 

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

If you keep full names somewhere, it is best to avoid relative imports for these classes.

+2
Apr 01 '10 at 23:50
source share

Some people (e.g., https://stackoverflow.com/a/312947/ ) say that __qualname__ better than __name__ . Here is an example that shows the difference:

 $ cat dummy.py class One: class Two: pass $ 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 dummy.One >>> print(full_name_with_name(dummy.One.Two)) # Wrong dummy.Two >>> print(full_name_with_qualname(dummy.One)) # Correct dummy.One >>> print(full_name_with_qualname(dummy.One.Two)) # Correct dummy.One.Two 

Note that this also works correctly for buildins:

 >>> print(full_name_with_qualname(print)) builtins.print >>> import builtins >>> builtins.print <built-in function print> 
+2
May 22 '18 at 10:23
source share

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

 def get_qualified_python_name_from_class(model): c = model.__class__.__mro__[0] name = c.__module__ + "." + c.__name__ return name 
0
Feb 24 '17 at 19:32
source share

For python3.7 I use:

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

Receiving:

 package.subpackage.ClassName 
0
May 21 '19 at 10:28
source share



All Articles