Python Notes and Examples

OOP

#!/usr/bin/env python3

class Foo:
    a = 55  # class attribute

    # initializer method (callable class attribute)
    def __init__(self, x):
        self.x = x
    
    # another method (callable class attribute)
    def f(self):
        print(Foo.a, self.x)


class Bar(Foo):
    b = 77

    def __init__(self, x, y):
        super().__init__(x)
        self.y = y
    
    def g(self):
        print(Foo.a, Bar.b, self.x, self.y)

        
def main():
    foo = Foo(3)
    print(Foo.a) # 55
    print(foo.x) #  3
    foo.f() # 55 3

    print('---')
    bar = Bar(4, 5)
    print(Bar.a) # 55
    print(Bar.b) # 77
    print(bar.x) #  4
    print(bar.y) #  5
    bar.g() # 55 77 4 5
    
if __name__ == '__main__':
    main()

Notes: