Slide 31
Slide 31 text
Reuven M. Lerner • @reuvenmlerner • https://lerner.co.il
Understanding Attributes
class Person:
population = 0 # not a variable definition!
def __init__(self, name):
Person.population += 1
self.name = name
print(f'Before, {Person.population=}')
p1 = Person('name1')
p2 = Person('name2')
print(f'After, {Person.population=}')
print(f'After, {p1.population=}')
print(f'After, {p2.population=}')
Let’s walk through this
31
I: Does Person have the
attribute “population”? YES, 2
I: Does p1 have the
attribute “population”? NO.
C: Does Person have the
attribute “population”? YES, 2
I: Does p2 have the
attribute “population”? NO.
C: Does Person have the
attribute “population”? YES, 2