pg.as_sealed

Accessible via pg.as_sealed, pg.symbolic.as_sealed.

as_sealed(sealed=True)[source]

Returns a context manager to treat symbolic values as sealed/unsealed.

While the user can use Symbolic.seal to seal or unseal an individual object. This context manager is useful to create a readonly zone for operations on all existing symbolic objects.

This function is thread-safe and can be nested. In the nested use case, the sealed flag of immediate parent context is effective.

Example:

sd1 = pg.Dict()
sd2 = pg.Dict().seal()

with pg.as_sealed(True):
  sd1.a = 2  # NOT OK
  sd2.a = 2  # NOT OK
  with pg.as_sealed(False):
    sd1.a = 2   # OK
    sd2.a = 2  # OK
    with pg.as_sealed(None):
      sd1.a = 1  # OK
      sd2.a = 1  # NOT OK
Return type:

ContextManager[None]

Parameters:

sealed – If True, treats all symbolic values as sealed in scope. If False, treats all as unsealed. If None, honor object-level sealed state.

Returns:

A context manager that treats all symbolic values as sealed/unsealed

in scope. After leaving the scope, the sealed state of individual objects will remain intact.