* В Python классы могут быть вложенными import MySQLdb # Singleton class Singleton: def __init__(self, cls): self.cls = cls self.instance = None def __call__(self, *args, **kwds): if self.instance == None: self.instance = self.cls(*args, **kwds) return self.instance @Singleton class Database: connection = None def get_connection(self): if self.connection is None: self.connection = MySQLdb.connect( host="localhost", user="root", passwd="111", db="test") return self.connection db1 = Database().get_connection() db2 = Database().get_connection()
Python Справочник v0.05 © 2007-2024 Igor Salnikov aka SunDoctor