Use a class decorator to implement late-initialization
问题 I am using some classes which need to connect to databases. The connection is only really needed when performing a real action. I want to delay the connection phase until it is really needed. For that, I want to do something similar to this: class MyClass def __init__(self): self.conn = None def connect(self): if self.conn : return self.conn = ConnectToDatabase() @connect def do_something1(self): self.conn.do_something1() @connect def do_something2(self): self.conn.do_something2() But I do