python class DatabaseConnection: def __init__(self, host, port, username, password): self.host = host self.port = port self.username = username self.password = password self.connection = None def __enter__(self): self.connection = self.connect_to_database() return self.connection def __exit__(self, exc_type, exc_val, exc_tb): self.connection.close() def connect_to_database(self): pass with DatabaseConnection('localhost', 5432, 'admin', 'password') as db: pass


上一篇:
下一篇:
切换中文