0

i have seen a youtuber writing this code without knowing a part of it and he won't reply on the comment section so i copied the code for someone to explain it here. why He accessed the class virable gender in display method via {self.gender} rather than {Man.gender} since accessing class variable through className.VariableName ? isnt that true ?

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def display(self):
       return f"name is {self.name} and age is '{self.age}' "
    
class Man(Person):
    gender = 'male'
    no_of_men = 0
    def __init__(self, name, age, voice):
        super().__init__(name,age)
        self.voice = voice
        Man.no_of_men += 1

    def display(self):
        string = super().display()
        return string + f"and voice is {self.voice} and gender is {self.gender}"

man = Man("name", 20, "harsh")
print(man.display())
print(Man.no_of_men)
momoshki
  • 23
  • 4

0 Answers0