最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to inherit values from a parent class without specifying the attributes in a child class - Stack Overflow

programmeradmin3浏览0评论

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).

The task is:

Create classes "father" and "mother" with some attributes equal and some of them - different; then create a "child" class that would be inherited from both previous classes to make their equal attributes be inherited by "child" class from the "father" class (this is about priority). Create an object of the "child" class and return its attributes in the output.

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论