Skip to content

Advanced object-oriented: Possible solution for Eye Color Exercise #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
HaBeSte opened this issue May 9, 2025 · 2 comments
Open

Advanced object-oriented: Possible solution for Eye Color Exercise #322

HaBeSte opened this issue May 9, 2025 · 2 comments
Labels
advanced tutorial clarification Something to expand or make clearer

Comments

@HaBeSte
Copy link

HaBeSte commented May 9, 2025

class Eye:
    def __init__(self, color):
        self.color = color

class Mother(Eye):
    def __init__(self, color):
        super().__init__(color)

class Father(Eye):
    def __init__(self, color):
        super().__init__(color)

class Child(Eye):
    def __init__(self, mom: Mother, dad: Father):
        result = "blue"
        if "brown" in [mom.color, dad.color]:
            result = "brown" 
        super().__init__(result)

mom = Mother("blue")
dad = Father("blue")
kid = Child(mom, dad)
@edoardob90 edoardob90 added clarification Something to expand or make clearer advanced tutorial labels May 10, 2025
@edoardob90
Copy link
Member

Thanks @HaBeSte! Could you just add a brief description of the reason behind this suggestion?

I would probably suggest to create a Human class instead with some "default" attributes. Otherwise this becomes more an example of "composition" rather than "inheritance".

Composition would be natural in this case:

class Mother:
    def __init__(self, eye_color: str):
        self.eye_color = eye_color


class Father:
    def __init__(self, eye_color: str):
        self.eye_color = eye_color


class Child:
    def __init__(self, mother: Mother, father: Father):
        self.mother = mother
        self.father = father
        self.eye_color = self.set_eye_color()

    def set_eye_color(self):
        """Set Child eye color based on Mother and Father eye color"""
        return self.mother.eye_color + self.father.eye_color


m = Mother("blu")
f = Father("green")
print(Child(m, f).eye_color)

@HaBeSte
Copy link
Author

HaBeSte commented May 12, 2025

Hi @edoardob90
As I understood the idea behind the task was to see the inheritance as an example, using an example where in real life inheritance played a role. The initial solution bothered me a bit, since it used variables called mother_eye_color, resp. father_eye_color and super() couldn't be used directly, because of the double inheritance. So I sat together with @despadam and we came up with the solution above.
I see your point, when we use "eye" (or human) for inheritance, again it's not really the inheritance from mom and dad as it initially supposed to be the idea. maybe a class called "genes" would make more sense in that context?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
advanced tutorial clarification Something to expand or make clearer
Projects
None yet
Development

No branches or pull requests

2 participants