# Sub initializer # The following output is when we call super class's __init__ with super().__init__() # Base initializer # Sub initializer s.f() # Sub.f() """ Other languages automatically call base class initializers. But Python treats __init__() like any other method. Base class __init__() is not called if overridden. To call base class's __init__() along with subclass's __init__() use super() """ from sorted_list import SortedList sl = SortedList([4, 3, 2, 1]) print(sl) # SortedList([1, 2, 3, 4]) print(len(sl)) # 4 sl.add(-42) print(sl) # SortedList([-42, 1, 2, 3, 4]) """ isinstance() --> determinses if an object is of a specified type --> used for run time type checking """ # Examples isinstance(3, int) # => True x = [] isinstance(x, (float, dict, list)) # => True """ CHECKING FOR SPECIFIC TYPES IS BAD DESIGN IN PYTHON """
# 09_02-A Realistic Example SortedList from sorted_list import SortedList sl = SortedList([4, 3, 78, 11]) sl len(sl) sl.add(-42) sl sl.add(7) sl from sorted_list import IntList il = IntList([1, 2, 3, 4]) il il.add(19) il # TypeError: IntList only supports integer values. il.add('5')
# 09_02-A Realistic Example SortedList from sorted_list import SortedList sl = SortedList([4, 3, 78, 11]) sl len(sl) sl.add(-42) sl sl.add(7) sl from sorted_list import IntList il = IntList([1,2,3,4]) il il.add(19) il # TypeError: IntList only supports integer values. il.add('5')