Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Class count instances

class Thing:
    count = 0
    def __init__(self):
        Thing.count += 1

def main():
    print(Thing.count)  # 0
    t1 = Thing()
    print(Thing.count)  # 1
    t2 = Thing()
    print(Thing.count)  # 2
    t3 = Thing()
    print(Thing.count)  # 3
    t3 = None
    print(Thing.count)  # 3

main()
print(Thing.count)  # 3