[Python-ideas] PEP 3155 - Qualified name for classes and functions

Nick Coghlan ncoghlan at gmail.com
Sun Oct 30 01:52:15 CEST 2011


On Sun, Oct 30, 2011 at 8:18 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
>
> Hello,
>
> I would like to propose the following PEP for discussion and, if
> possible, acceptance. I think the proposal shouldn't be too
> controversial (I find it quite simple and straightforward myself :-)).

Indeed (and I believe this should make the next version of the module
aliasing PEP substantially shorter!).

> Proposal
> ========
>
> This PEP proposes the addition of a ``__qname__`` attribute to functions
> and classes.  For top-level functions and classes, the ``__qname__``
> attribute is equal to the ``__name__`` attribute.  For nested classed,
> methods, and nested functions, the ``__qname__`` attribute contains a
> dotted path leading to the object from the module top-level.

I like '__qname__'. While I'm sympathetic to the suggestion of the
more explicit '__qualname__', I actually prefer the idea of adding
"qname" as an official shorthand for "qualified name" in the glossary.

> Example with nested classes
> ---------------------------
>
>>>> class C:
> ...   def f(): pass
> ...   class D:
> ...     def g(): pass
> ...
>>>> C.__qname__
> 'C'
>>>> C.f.__qname__
> 'C.f'
>>>> C.D.__qname__
> 'C.D'
>>>> C.D.g.__qname__
> 'C.D.g'
>
> Example with nested functions
> -----------------------------
>
>>>> def f():
> ...   def g(): pass
> ...   return g
> ...
>>>> f.__qname__
> 'f'
>>>> f().__qname__
> 'f.g'

For nested functions, I suggest adding something to the qname to
directly indicate that the scope is hidden. Adding parentheses to the
name of the outer function would probably work:

    f().g

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list