[Python] How to inherit values from a parent class without specifying the attributes in a...

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I have a task to recreate a human parent-children connection via OOP and inheritance. So, there are classes for father and mother, and there is also a class for children, which should inherit firstly from the father class, then from the mother class (also I inserted some additional attributes, absent in parents' classes).

    Code (unfinished):

    class Father:
    def __init__(self, surname, name, age, size):
    self.surname = surname
    self.name = name
    self.age = age
    if size in range(5, 31):
    self.__d_size = size

    @property
    def d_size(self):
    return self.__d_size

    @d_size.setter
    def d_size(self, size):
    if size in range(5, 31):
    self.__d_size = size


    class Mother:
    def __init__(self, surname, name, age, size):
    self.surname = surname
    self.name = name
    self.age = age
    if size in range(1, 7):
    self.__b_size = size

    @property
    def b_size(self):
    return self.__b_size

    @b_size.setter
    def b_size(self, size):
    if size in range(1, 7):
    self.__b_size = size


    class Child(Father, Mother):

    @property
    def gender(self):
    # don't bother guessing the principle of the formula and what do "d" and "b" mean here
    if (super().d_size + super().b_size) % 2 != 0:
    gender = "male"
    else:
    gender = "female"
    return gender

    @property
    def patronymic(self):
    if self.gender == "male":
    return super().name + "vich"
    else:
    return super().name + "vna"

    @property
    def full_name(self):
    return f"{self.surname} {self.name} {self.patronymic}, {self.age} years old, {self.gender}"


    if __name__ == '__main__':
    alex = Father('Romanov', 'Alexey', 46, 20)
    nata = Mother('Naryshkina', 'Natalya', 24, 3)
    peter = Child('Peter', 3, 5) # <- surname isn't meant to be specified here
    peter.full_name # expected output here is "Romanov Peter Alexeyvich, 3 years old, male"


    Questions:

    1. Is there a possibility for the Child class to inherit a surname value from the Father's class without a need to specify it while creating an object of the Child class? Because from the point of common logic it seems redundant to specify the surname for the Child object, if it is obvious that Father surname is inherited. I suspect that something has to be added here, but I don't know whether it requires to redefine __init__ in Child class or something else to be done.
    2. I'm slightly lost in searching of a better method to validate values given for attributes before the creation of an object. Here in parents' classes I've implemented it in two ways: directly in __init__ via if and then in @property. Because some values here have to make at least some sense, as the one hardly can bear children before turning 12 y.o., so I could also insert here an embedded check for a value assigned to self.age attribute. But I'm not sure how to do it better. I guess that @property works only for changing of the value of an already existing and initialised attribute; so, it wouldn't be helpful in my case.

    Continue reading...

Compartilhe esta Página