Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Counter:
- pass
- Counter # class object
- x = Counter() # создадим объект класса - instance object (экземпляр класса)
- x.count = 0 # создаем новый атрибут у нашего объекта instance object
- x.count += 1 # изменим его "на месте"
- # создадим класс с конструктором, атрибутом и методами
- class Counter(object):
- def __init__(self):
- self.count = 0
- def inc(self):
- self.count += 1
- def reset(self):
- self.count = 0
- Counter # class object
- x = Counter()
- x.inc() # связанный метод или Bound Method
- print(x.count) # 1
- Counter.inc(x) # этот метод абсолютно эквивалентен x.inc() ведь в качестве self ставится x
- print(x.count) # 2
- x.reset()
- print(x.count) # 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement