Implementing the decorator pattern in Python

后端 未结 7 1597
梦谈多话
梦谈多话 2020-12-02 09:59

I want to implement the decorator pattern in Python, and I wondered if there is a way to write a decorator that just implements the function it wants to modify, without writ

7条回答
  •  情话喂你
    2020-12-02 10:45

    It's arguably not the best practice, but you can add functionality to instances, as I've done to help transition my code from Django's ORM to SQLAlachemy, as follows:

    def _save(self):
        session.add(self)
        session.commit()
    setattr(Base,'save',_save)
    

提交回复
热议问题