Advertisement
Dmitry_Dronov

class counter

Apr 25th, 2016
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. class Counter:
  2.     pass
  3. Counter # class object
  4. x = Counter() # создадим объект класса - instance object (экземпляр класса)
  5. x.count = 0 # создаем новый атрибут у нашего объекта instance object
  6. x.count += 1 # изменим его "на месте"
  7.  
  8. # создадим класс с конструктором, атрибутом и методами
  9. class  Counter(object):
  10.     def __init__(self):
  11.         self.count = 0
  12.     def inc(self):
  13.         self.count += 1
  14.     def reset(self):
  15.         self.count = 0
  16. Counter # class object
  17. x = Counter()
  18. x.inc() # связанный метод или Bound Method
  19. print(x.count) # 1
  20. Counter.inc(x) # этот метод абсолютно эквивалентен x.inc() ведь в качестве self ставится x
  21. print(x.count) # 2
  22. x.reset()
  23. print(x.count) # 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement