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
Add a comment  | 

1 Answer 1

Reset to default -2

What 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