nanoutils.SetAttr

class nanoutils.SetAttr(obj, name, value)[source]

A context manager for temporarily changing an attribute’s value.

The SetAttr context manager is thread-safe, reusable and reentrant.

Warning

Note that while SetAttr.__enter__() and SetAttr.__exit__() are thread-safe, the same does not hold for SetAttr.__init__().

Examples

>>> from nanoutils import SetAttr

>>> class Test:
...     a = False

>>> print(Test.a)
False

>>> set_attr = SetAttr(Test, 'a', True)
>>> with set_attr:
...     print(Test.a)
True
__init__(obj, name, value)[source]

Initialize the SetAttr context manager.

Parameters:
Return type:

None

__repr__()[source]

Implement str(self) and repr(self).

__eq__(value)[source]

Implement self == value.

__reduce__()[source]

A helper for pickle.

Warning

Unsupported operation, raises a TypeError.

__copy__()[source]

Implement copy.copy(self).

__deepcopy__(memo=None)[source]

Implement copy.deepcopy(self, memo=memo).

__hash__()[source]

Implement hash(self).

Warning

A TypeError will be raised if SetAttr.value is not hashable.

__enter__()[source]

Enter the context manager, modify SetAttr.obj.

__exit__(exc_type, exc_value, traceback)[source]

Exit the context manager, restore SetAttr.obj.

property obj

Get the to-be modified object.

Type:

object

property name

Get the name of the to-be modified attribute.

Type:

str

property value

Get the value to-be assigned to the name attribute of SetAttr.obj.

Type:

object

property attr

Get or set the name attribute of SetAttr.obj.

Type:

object