'Python Turtle class inheritance not working

I am trying to create a snake with an edited Turtles class. So I create a SnakeSegment class which has Turtle class as a parent. I then add a variable to the SnakeSegment class and also I set things like the colour and speed of the Turtle.

I then create my snake class where I bring in my SnakeSegments. The problem is the snake segments don't bring in their colour which I initiated in the snake segment class.

I think I have got the inheritance wrong, but I am not sure.

I have 2 files: components.py and main.py

components.py:

from turtle import Turtle


class SnakeSegment(Turtle):
    def __init__(self, givenListPosition):
        super().__init__()
        self.segment = Turtle()
        self.segment.shape("square")
        self.segment.color('white')
        self.segment.speed(1)
        self.segment.penup()
        self.listPosition = givenListPosition


class Snake:
    def __init__(self, screen):
        self.theScreen = screen
        self.pastPositions = [(0.00, 0.00), (-20.00, 0.00), (-30.00, 0.00)]
        self.t1 = SnakeSegment(0)
        self.t2 = SnakeSegment(1)
        self.t2.goto(self.pastPositions[int(self.t2.listPosition)])
        self.t3 = SnakeSegment(2)
        self.t2.goto(self.pastPositions[int(self.t3.listPosition)])

main.py:

from components import *
from turtle import Screen

screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake Game")

snake = Snake(screen)
snake.theScreen.exitonclick()


Solution 1:[1]

I'm not sure I get your final goal and you did not show how your Turtle class looks. However, it appears you are using a mix of inheritance and composition which doesn't seem correct whatever your goal is. You have to decide if SnakeSegment is a Turtle (inheritance) or has a Turtle (composition) in your data model.

With inheritance, SnakeSegment will have all methods and attributes of the base Turtle class (which I assume has some of the "shape", "color" etc methods) and will "be" a Turtle from any point of view. You could then write something like this

class SnakeSegment(Turtle):
    def __init__(self, givenListPosition):
        super().__init__()
        self.shape("square")
        self.color('white')
        self.speed(1)
        self.penup()
        self.listPosition = givenListPosition

With composition instead, SnakeSegment doesn't inherit from anything, it just has a member which is an instance of the Turtle class. In that case, something like that would be used

class SnakeSegment:
    def __init__(self, givenListPosition):
        self.segment = Turtle()
        self.segment.shape("square")
        self.segment.color('white')
        self.segment.speed(1)
        self.segment.penup()
        self.listPosition = givenListPosition

I'd suggest you start from that and before taking a decision on your data model, have a read about mixins as well (i.e. https://www.pythontutorial.net/python-oop/python-mixin/)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 benelgiac