<jupyter_start><jupyter_text>Inheritance - instead of duck typing<jupyter_code>class Animal:
    def __init__(self):
        self.age = 99
    def speak(self):
        print("unknown")

class Dog(Animal):
    def speak(self):
        print("Bau")
        # super().speak()
        
d = Dog()
a = super(Dog,d).speak()
d.speak()
d.age
help(super)<jupyter_output><empty_output><jupyter_text>Method resolution order<jupyter_code>class A:
    def __init__(self):
        self._score = 42
        self.__find_this = 99 # kind of private
    def foo(self):
        print("A foo")
        
class B(A):
    def foo(self):
        print("B foo")
        
class C(A):
    def foo(self):
        print("C foo")
        
class D(A):
    def foo(self):
        print("D foo")
        
class E(B,C,D):
    def foo(self):
        print("E foo")
        print("_score", self._score)
        #print("__find_this", self.__find_this)
        print("__find_this", self._A__find_this)
        
    def bar(self):
        self.foo() 
        super().foo() 
        D.foo(self)   # unbound method --> you have to pass self
        A.foo(self)
        print(self._score)

e = E()
e.bar()
print (E.__mro__)<jupyter_output><empty_output><jupyter_text>Use `isinstance` and `issubclass`, instead of `type(x) == type(y)`<jupyter_code>print(isinstance(e,A))
print(isinstance(e,dict))
print(isinstance(B,A))
print(issubclass(B,A))<jupyter_output><empty_output><jupyter_text>Abstract classes?- Abstract Base Class (ABC)<jupyter_code>import abc


class Animal(abc.ABC): # Python > 3.4
    def __init__(self,age,name):
        self.age = age
        self.name = name
    @abc.abstractmethod
    def speak(self): pass

a = Animal() # expect error
# class Animal(metaclass = abc.ABCMeta): # 2 < Python < 3.4

# Python 2
# class Animal(object):
#     __metaclass__ = abc.ABCMeta


class Dog(Animal):
    def speak(self):
        print("Bau")
d = Dog(1,"Fido")
d.speak()
print(d.age, d.name)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14833501513622604, 'fraction_numerical': 0.006559031281533804, 'mean_word_length': 2.895874263261297, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '111044', 'license': 'MIT', 'n_tokens_mistral': 736, 'n_tokens_neox': 715, 'n_words': 147, 'path': 'lectures/lectures/python/03_advanced_topics/04_inheritance.ipynb', 'repo_name': 'wilsonjefferson/DSSC_AP', 'size': '1982'}