MarkImmutable

(class from pyomo.common.config)

class pyomo.common.config.MarkImmutable(*args)[source]

Bases: object

Mark instances of ConfigValue as immutable.

Parameters:

config_value (ConfigValue) – The ConfigValue instances that should be marked immutable. Note that multiple instances of ConfigValue can be passed.

Examples

config = ConfigDict()
config.declare('a', ConfigValue(default=1, domain=int))
config.declare('b', ConfigValue(default=1, domain=int))
locker = MarkImmutable(config.get('a'), config.get('b'))

Now, config.a and config.b cannot be changed:

>>> config.a = 5
Traceback (most recent call last):
   ...
RuntimeError: ConfigValue 'a' is currently immutable
>>> print(config.a)
1

To make them mutable again,

>>> locker.release_lock()
>>> config.a = 5
>>> print(config.a)
5

Note that this can be used as a context manager as well:

>>> with MarkImmutable(config.get('a'), config.get('b')):
...     config.a = 10
Traceback (most recent call last):
   ...
RuntimeError: ConfigValue 'a' is currently immutable
>>> print(config.a)
5
>>> config.a = 10
>>> print(config.a)
10
__init__(*args)[source]

Methods

__init__(*args)

lock()

release_lock()

Member Documentation