GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing peps.python.org — PEP 3155 – Qualified name for classes and functions | peps.python.org
PEP 3155 – Qualified name for classes and functions
PEP 3155 – Qualified name for classes and functions
RationalePython’s introspection facilities have long had poor support for nested classes. Given a class object, it is impossible to know whether it was defined inside another class or at module top-level; and, if the former, it is also impossible to know in which class it was defined. While use of nested classes is often considered poor style, the only reason for them to have second class introspection support is a lousy pun. Python 3 adds insult to injury by dropping what was formerly known as unbound methods. In Python 2, given the following definition: class C: def f(): pass you can then walk up from the >>> C.f.im_class <class '__main__.C'> This possibility is gone in Python 3: >>> C.f.im_class Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'function' object has no attribute 'im_class' >>> dir(C.f) ['__annotations__', '__call__', '__class__', '__closure__', '__code__', '__defaults__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__globals__', '__gt__', '__hash__', '__init__', '__kwdefaults__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__'] This limits again the introspection capabilities available to the user. It can produce actual issues when porting software to Python 3, for example Twisted Core where the issue of introspecting method objects came up several times. It also limits pickling support [1]. ProposalThis PEP proposes the addition of a The repr() and str() of functions and classes is modified to use
Example with nested classes>>> class C: ... def f(): pass ... class D: ... def g(): pass ... >>> C.__qualname__ 'C' >>> C.f.__qualname__ 'C.f' >>> C.D.__qualname__ 'C.D' >>> C.D.g.__qualname__ 'C.D.g' Example with nested functions>>> def f(): ... def g(): pass ... return g ... >>> f.__qualname__ 'f' >>> f().__qualname__ 'f.<locals>.g' LimitationsWith nested functions (and classes defined inside functions), the
dotted path will not be walkable programmatically as a function’s
namespace is not available from the outside. It will still be more
helpful to the human reader than the bare As the DiscussionExcluding the module nameAs Reviving unbound methodsReviving unbound methods would only solve a fraction of the problems this PEP solves, at a higher price (an additional object type and an additional indirection, rather than an additional attribute). Naming choice“Qualified name” is the best approximation, as a short phrase, of what the
additional attribute is about. It is not a “full name” or “fully qualified
name” since it (deliberately) does not include the module name. Calling
it a “path” would risk confusion with filesystem paths and the The first proposal for the attribute name was to call it ReferencesCopyrightThis document has been placed in the public domain. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |