How does "__doc__ % globals()" work?
Ethan Furman
ethan at stoneleaf.us
Mon Apr 12 19:39:08 EDT 2021
On 4/12/21 3:06 PM, Jaime wrote:
> Hi all. Line 102 of https://github.com/python/peps/blob/master/pep2html.py says:
>
> print(__doc__ % globals(), file=out)
>
> I realise that globals() is a standard-library
> built-in function that returns a dictionary representing the current
> global symbol table, and that the result of this expression is the
> value of the dictionary where the key is "__doc__"
This is not correct.
There are a couple ways to use %-interpolation:
some_name = 'Jaime'
# simple
`hello, %s` % (some_name, ) # hello, Jaime
# keyed
`hello, %(a_name)s` % {'a_name': 'Ethan'} # hello, Ethan
What you are seeing is the keyed version. So, if the module had, for example:
version = 1.3
author = 'GvR'
date = '2021-04-12'
and the doc string was
__doc__ = "version %(version)s was initially created by %(author)s"
then
__doc__ % globals()
would substitute the `author` and `version` keys into __doc__, and printing that would yield
version 1.3 was initially created by GvR
Note that extra keys are ignored.
--
~Ethan~
More information about the Python-list
mailing list