Python Glossary Русский

Python 3.2+configparserterm

configparser.ExtendedInterpolation

interpolation

Interpolation in the ${section:option} style: it inserts values from any section and leaves % as an ordinary character.

Syntax

configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())

It is enabled only explicitly and only as an instance: interpolation=ExtendedInterpolation(). Without the 'section:' part it looks in the current section and [DEFAULT]. Here the dollar sign is escaped ($$), while percents and %(x)s stay literal, which helps when values contain many percents. Broken references, as with the basic flavour, surface on get().

Examples

import configparser
cfg = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
cfg.read_string("[common]\nroot = /srv\n[app]\nlogs = ${common:root}/logs\nrate = 50%\nprice = $$5\n")
print(cfg.get("app", "logs"))  # → /srv/logs
print(cfg.get("app", "rate"), cfg.get("app", "price"))  # → 50% $5

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy