Settle on __qualname__ since __qname__ is non-obvious for some people.

This commit is contained in:
Antoine Pitrou 2011-11-18 20:50:49 +01:00
parent 8b65016bca
commit 93b3583016
1 changed files with 29 additions and 11 deletions

View File

@ -59,16 +59,16 @@ objects came up several times. It also limits pickling support [1]_.
Proposal Proposal
======== ========
This PEP proposes the addition of a ``__qname__`` attribute to This PEP proposes the addition of a ``__qualname__`` attribute to
functions and classes. For top-level functions and classes, the functions and classes. For top-level functions and classes, the
``__qname__`` attribute is equal to the ``__name__`` attribute. For ``__qualname__`` attribute is equal to the ``__name__`` attribute. For
nested classed, methods, and nested functions, the ``__qname__`` nested classed, methods, and nested functions, the ``__qualname__``
attribute contains a dotted path leading to the object from the module attribute contains a dotted path leading to the object from the module
top-level. A function's local namespace is represented in that dotted top-level. A function's local namespace is represented in that dotted
path by a component named ``<locals>``. path by a component named ``<locals>``.
The repr() and str() of functions and classes is modified to use The repr() and str() of functions and classes is modified to use
``__qname__`` rather than ``__name__``. ``__qualname__`` rather than ``__name__``.
Example with nested classes Example with nested classes
--------------------------- ---------------------------
@ -78,13 +78,13 @@ Example with nested classes
... class D: ... class D:
... def g(): pass ... def g(): pass
... ...
>>> C.__qname__ >>> C.__qualname__
'C' 'C'
>>> C.f.__qname__ >>> C.f.__qualname__
'C.f' 'C.f'
>>> C.D.__qname__ >>> C.D.__qualname__
'C.D' 'C.D'
>>> C.D.g.__qname__ >>> C.D.g.__qualname__
'C.D.g' 'C.D.g'
Example with nested functions Example with nested functions
@ -94,9 +94,9 @@ Example with nested functions
... def g(): pass ... def g(): pass
... return g ... return g
... ...
>>> f.__qname__ >>> f.__qualname__
'f' 'f'
>>> f().__qname__ >>> f().__qualname__
'f.<locals>.g' 'f.<locals>.g'
@ -108,16 +108,34 @@ dotted path will not be walkable programmatically as a function's
namespace is not available from the outside. It will still be more namespace is not available from the outside. It will still be more
helpful to the human reader than the bare ``__name__``. helpful to the human reader than the bare ``__name__``.
As the ``__name__`` attribute, the ``__qname__`` attribute is computed As the ``__name__`` attribute, the ``__qualname__`` attribute is computed
statically and it will not automatically follow rebinding. statically and it will not automatically follow rebinding.
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 ``__file__``
attribute.
The first proposal for the attribute name was to call it ``__qname__`` but
many people (who are not aware of previous use of such jargon in e.g. the
XML specification [2]_) found it obscure and non-obvious, which is why the
slightly less short and more explicit ``__qualname__`` was finally chosen.
References References
========== ==========
.. [1] "pickle should support methods": .. [1] "pickle should support methods":
http://bugs.python.org/issue9276 http://bugs.python.org/issue9276
.. [2] "QName" entry in Wikipedia:
http://en.wikipedia.org/wiki/QName
Copyright Copyright
========= =========