nanoutils.SetAttr¶
- class nanoutils.SetAttr(obj, name, value)[source]
A context manager for temporarily changing an attribute’s value.
The
SetAttrcontext manager is thread-safe, reusable and reentrant.Warning
Note that while
SetAttr.__enter__()andSetAttr.__exit__()are thread-safe, the same does not hold forSetAttr.__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
SetAttrcontext manager.- Parameters:
obj (
object) – The to-be modified object. SeeSetAttr.obj.name (
str) – The name of the to-be modified attribute. SeeSetAttr.name.value (
object) – The value to-be assigned to the name attribute of obj. SeeSetAttr.value.
- Return type:
- __repr__()[source]¶
Implement
str(self)andrepr(self).
- __eq__(value)[source]¶
Implement
self == value.
- __copy__()[source]¶
Implement
copy.copy(self).
- __deepcopy__(memo=None)[source]¶
Implement
copy.deepcopy(self, memo=memo).
- __hash__()[source]¶
Implement
hash(self).Warning
A
TypeErrorwill be raised ifSetAttr.valueis 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:
- property name
Get the name of the to-be modified attribute.
- Type:
- property value
Get the value to-be assigned to the
nameattribute ofSetAttr.obj.- Type:
- property attr
Get or set the
nameattribute ofSetAttr.obj.- Type: