class Database: connection = 4 print "Class Start" def __init__(self): print "Class Init!" def connect_dbs(self): if self.connection is not None: self.connection = self.connection + 1 return self.connection else: self.connection = 1 print "Initial Connecting" return self.connection print "Start" U = Database() for i in range(10): print U.connect_dbs() print "One more" U2 = Database() for i in range(10): print U2.connect_dbs() print Database.connection
Class Start Start Class Init! 5 6 7 8 9 10 11 12 13 14 One more Class Init! 5 6 7 8 9 10 11 12 13 14 4
Почему в вышеописанном случае переменная инстанса инициализируется переменной класса, а переменная класса не меняется :)