随着以下程序
#! /usr/bin/env pythonimport foodef fullname(o): # o.__module__ + "." + o.__class__.__qualname__ is an example in # this context of H.L. Mencken's "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)
并
Bar定义为
class Bar(object): def __init__(self, v=42): self.val = v
输出是
$ ./prog.pyfoo.Bar



