0

Possible Duplicate:
How to use variables already defined in ConfigParser

For example, I have the following config file:

[Sec1]
opt1 = 1

[Sec2]
opt2 = 1

I want to set opt2 equal to opt1 explicitly, so that if I update opt1 later, opt2 will be updated as well.

But I cannot find a way to do it.

Community
  • 1
  • 1
Derrick Zhang
  • 21,201
  • 18
  • 53
  • 73
  • 1
    Duplicate of [4999190](http://stackoverflow.com/q/4999190/). Unfortunately the answer is: reference expansion only works inside the same section, not across sections. – Lukas Graf Sep 17 '12 at 05:22

1 Answers1

2

You can use interpolation with either ConfigParser or SafeConfigParser. However, you cannot interpolate between sections in the configuration, unless one of them is DEFAULT:

[DEFAULT]
opt1 = 1

[Sec2]
opt2 = %(opt1)s

It's important to note that the DEFAULT options mask any further attempt to use them in other sections, and that you cannot re-assign opt1 via Sec2.opt2.

Matthew Trevor
  • 14,354
  • 6
  • 37
  • 50