admin管理员组文章数量:1123231
Considering the following class and object:
class foo:
def __init__(self,name):
self.name = name
def print_name(self):
print(self.name)
object1 = foo('old_name')
I want to run a block of code that will perform some actions and change the object attributes, but in the end I would like to get the original object back as it was. Or, in other words, I want to have a temporary copy of the object to perform some actions on, and then release it.
With the following contextmanager it seems to work. I copied the object for the sake of typing as the object may have many attributes, that can all be temporarily changed, but I could just store all the possible attributes and restore them in a similar way.
However, is there a better way of doing this?
class foo:
def __init__(self,name):
self.name = name
def print_name(self):
print(self.name)
@contextmanager
def dummy(self):
try:
dummy = copy.copy(self)
yield dummy
finally:
pass
object1 = foo('old_name')
object1.print_name()
old_name
with object1.dummy() as d:
d.name = 'new_name'
d.print_name()
new_name
object1.print_name()
old_name
Considering the following class and object:
class foo:
def __init__(self,name):
self.name = name
def print_name(self):
print(self.name)
object1 = foo('old_name')
I want to run a block of code that will perform some actions and change the object attributes, but in the end I would like to get the original object back as it was. Or, in other words, I want to have a temporary copy of the object to perform some actions on, and then release it.
With the following contextmanager it seems to work. I copied the object for the sake of typing as the object may have many attributes, that can all be temporarily changed, but I could just store all the possible attributes and restore them in a similar way.
However, is there a better way of doing this?
class foo:
def __init__(self,name):
self.name = name
def print_name(self):
print(self.name)
@contextmanager
def dummy(self):
try:
dummy = copy.copy(self)
yield dummy
finally:
pass
object1 = foo('old_name')
object1.print_name()
old_name
with object1.dummy() as d:
d.name = 'new_name'
d.print_name()
new_name
object1.print_name()
old_name
Share Improve this question asked 8 hours ago MiguelMiguel 91 bronze badge New contributor Miguel is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 2- 1 Why not just make a copy and work with that copy? – matszwecja Commented 8 hours ago
- This may happen multiple times in different ways, it is not practical to explicitly make a new, or rewrite, a copy each time. It is easier just to have a temporary and immediately disposable copy. – Miguel Commented 8 hours ago
1 Answer
Reset to default -2What you are doing above should work - but because you are not saying what you describe in the quetion: you are creating a copy of the object - which is preferrable to have the object being mutated inside a context, and then reverted once the context is over.
It is possible to do what you describe - to actually keep a record all attributes in an object, and restore those when the context that would modify and use the modified object is over. But it will be safer, easier and more efficient to simply create a new copy, as you are doing.
本文标签: pythonHow to temporarily change a class objectStack Overflow
版权声明:本文标题:python - How to temporarily change a class object? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736556514a1944585.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论