Skip to content

Commit

Permalink
Add example using built-in vars in 7.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
uatach committed May 12, 2024
1 parent eb04e27 commit 2166f58
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Doc/tutorial/inputoutput.rst
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,12 @@ notation. ::
Jack: 4098; Sjoerd: 4127; Dcab: 8637678

This is particularly useful in combination with the built-in function
:func:`vars`, which returns a dictionary containing all local variables.
:func:`vars`, which returns a dictionary containing all local variables. ::

>>> table = {k: str(v) for k, v in vars().items()}
>>> message = " ".join([f'{k}: ' + '{' + k +'};' for k in table.keys()])
>>> print(message.format(**table))
__name__: __main__; __doc__: None; __package__: None; __loader__: <class '_frozen_importlib.BuiltinImporter'>; __spec__: None; __annotations__: {}; __builtins__: <module 'builtins' (built-in)>;

As an example, the following lines produce a tidily aligned
set of columns giving integers and their squares and cubes::
Expand Down

0 comments on commit 2166f58

Please sign in to comment.