<jupyter_start><jupyter_text>Inheriting Variables and Methods<jupyter_code>CURRENR_YEAR = 2022

class Person:
    
    def __init__(self, name, year_born):
        self.name = name
        self.year_born = year_born
        
    def getAge(self):
        return CURRENR_YEAR - self.year_born
    
    def __str__(self):
        return f'{self.name}, {self.getAge()}'
alice = Person("<NAME>", 1990)
print(alice)<jupyter_output><NAME>, 32<jupyter_text>Want to create a class 'Student'with same attributes than 'Person' plus one attribite<jupyter_code>class Student:
    
    def __init__(self, name, year_born):
        self.name = name
        self.year_born = year_born
        self.knowledge = 0
        
    def study(self):
        self.knowledge += 1
        
    def getAge(self):
        return CURRENR_YEAR - self.year_born
    
    def __str__(self):
        return f'{self.name}, {self.getAge()}'
alice = Student("<NAME>", 1990)
alice.study()
print(alice.knowledge)<jupyter_output>1<jupyter_text>with inheritance :<jupyter_code>class Student(Person):
    
    def __init__(self, name, year_born):
        Person.__init__(self, name, year_born) #inheritance
        self.knowledge = 0

    def study(self):
        self.knowledge += 1
alice = Student("<NAME>", 1990)
alice.study()
print(alice.knowledge)
print(alice.getAge())<jupyter_output>1
32<jupyter_text>Invoking the Parent Class's Method<jupyter_code>from random import randrange

class Pet():
    boredom_decrement = 4
    hunger_decrement = 6
    boredom_threshold = 5
    hunger_threshold = 10
    sounds = ['Mrrp']
    def __init__(self, name = "Kitty", pet_type="dog"):
        self.name = name
        self.hunger = randrange(self.hunger_threshold)
        self.boredom = randrange(self.boredom_threshold)
        self.sounds = self.sounds[:]  # copy the class attribute, so that when we make changes to it, we won't affect the other Pets in the class
        self.pet_type = pet_type

    def clock_tick(self):
        self.boredom += 1
        self.hunger += 1

    def mood(self):
        if self.hunger <= self.hunger_threshold and self.boredom <= self.boredom_threshold:
            if self.pet_type == "dog": # if the pet is a dog, it will express its mood in different ways from a cat or any other type of animal
                return "happy"
            elif self.pet_type == "cat":
                return "happy, probably"
            else:
                return "HAPPY"
        elif self.hunger > self.hunger_threshold:
            if self.pet_type == "dog": # same for hunger -- dogs and cats will express their hunger a little bit differently in this version of the class definition
                return "hungry, arf"
            elif self.pet_type == "cat":
                return "hungry, meeeeow"
            else:
                return "hungry"
        else:
            return "bored"

    def __str__(self):
        state = "     I'm " + self.name + ". "
        state += " I feel " + self.mood() + ". "
        return state

    def hi(self):
        print(self.sounds[randrange(len(self.sounds))])
        self.reduce_boredom()

    def teach(self, word):
        self.sounds.append(word)
        self.reduce_boredom()

    def feed(self):
        self.reduce_hunger()

    def reduce_hunger(self):
        self.hunger = max(0, self.hunger - self.hunger_decrement)

    def reduce_boredom(self):
        self.boredom = max(0, self.boredom - self.boredom_decrement)
# the method feed will only return 'arf thanks'
class Dog(Pet):
    def feed(self):
        print('Arf! Thanks!')

# feed will return print and reduce hunger
class Dog(Pet):
    def feed(self):
        Pet.feed(self)
        print('Arf! Thanks!')
        
d1 = Dog('Astro')
print(d1.hunger)
d1.feed()
print(d1.hunger)<jupyter_output>7
Arf! Thanks!
1
{'fraction_non_alphanumeric': 0.10895248096613284, 'fraction_numerical': 0.010763980047256498, 'mean_word_length': 2.5050597976080957, 'pattern_counts': {'":': 4, '<': 19, '<?xml version=': 0, '>': 18, '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': '499712', 'license': 'Apache-2.0', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1224, 'n_words': 315, 'path': '02_Inheritance.ipynb', 'repo_name': 'ThomasFavrel/Python-Classes-and-Inheritance', 'size': '3829'}