pg.apply_wrappers¶
Accessible via pg.apply_wrappers
, pg.symbolic.apply_wrappers
.
- apply_wrappers(wrapper_classes=None, where=None)[source]¶
Context manager for swapping user classes with their class wrappers.
This helper method is a handy tool to swap user classes with their wrappers within a code block, without modifying exisiting code.
For example:
def foo(): return A() APrime = pg.wrap(A) with pg.apply_wrappers([APrime]): # Direct creation of an instance of `A` will be detoured to `APrime`. assert isinstance(A(), APrime) Indirect creation of an instance of `A` will be detoured too. assert isinstance(foo(), APrime) # Out of the scope, direct/indirect creation `of` A will be restored. assert not isinstance(A(), APrime) assert not isinstance(foo(), APrime)
pg.apply_wrappers
can be nested, under which the inner context will apply the wrappers from the outter context.pg.apply_wrappers
is NOT thread-safe.- Parameters:
wrapper_classes – Wrapper classes to use. If None, sets it to all registered wrapper classes.
where – An optional filter function in signature (wrapper_class) -> bool. If not None, only filtered wrapper_class will be swapped.
- Returns:
A context manager that detours the original classes to the wrapper classes.