Home > Software design >  Changing an attribute in the init method in python
Changing an attribute in the init method in python

Time:09-17

So in my snake class, I have initialized an attribute as self.x_cor = 0 Now, I want to modify or make use of this attribute in a different method (create_segment). It only works when I repeat the line of code (self.x_cor = 0 ) in the (create_segment) again.

If I don't repeat it, I get feedback that the snake object has no attribute as x_cor meanwhile I have initialized it in the init method

How do I use a different method to modify an attribute initialized in the init method?

import turtle
from turtle import Turtle
import random

MOVE_DISTANCE = 20
UP = 90
DOWN = 270
LEFT = 180
RIGHT = 0


class Snake:
    def __init__(self):
        self.speed = 9
        self.segments = []
        self.create_snake()
        self.x_cor = 0
        self.y_cor = 0
        # self.add_segment()
        self.head = self.segments[0]  # the first segment is designated as the head of the snake
        self.color_list = [(202, 164, 110), (240, 245, 241), (236, 239, 243), (149, 75, 50), (222, 201, 136),
                           (170, 154, 41), (138, 31, 20), (134, 163, 184), (197, 92, 73), (47, 121, 86), (73, 43, 35),
                           (145, 178, 149), (14, 98, 70), (232, 176, 165), (160, 142, 158), (54, 45, 50), (101, 75, 77),
                           (183, 205, 171), (36, 60, 74), (19, 86, 89), (82, 148, 129), (147, 17, 19), (27, 68, 102),
                           (12, 70, 64), (107, 127, 153), (176, 192, 208), (168, 99, 102), (53, 93, 123)]
        self.colors = ["red", "orange", "yellow", "blue", "green", "purple"]

    def create_snake(self):
        """ Creates the snake """
        for n in range(3):  # Creates 3 segments for now
            self.create_segment(n)

    def create_segment(self, n):
        """ Creates a new segment to be added to the snake """
        new_segment = Turtle("square")
        turtle.colormode(255)
        self.color_list = [(202, 164, 110), (240, 245, 241), (236, 239, 243), (149, 75, 50), (222, 201, 136),
                           (170, 154, 41), (138, 31, 20), (134, 163, 184), (197, 92, 73), (47, 121, 86), (73, 43, 35),
                           (145, 178, 149), (14, 98, 70), (232, 176, 165), (160, 142, 158), (54, 45, 50), (101, 75, 77),
                           (183, 205, 171), (36, 60, 74), (19, 86, 89), (82, 148, 129), (147, 17, 19), (27, 68, 102),
                           (12, 70, 64), (107, 127, 153), (176, 192, 208), (168, 99, 102), (53, 93, 123)]
        # new_segment.color(random.choice(self.color_list))
        self.colors = ["red", "orange", "yellow", "blue", "green", "purple"]
        new_segment.color(random.choice(self.colors))
        new_segment.penup()
        # new_segment.speed(9)
        self.x_cor = 0
        self.y_cor = 0
        new_segment.goto(self.x_cor, self.y_cor)
        self.x_cor -= 20  # Reduce the x_cor but maintain the y so all the segments will be on the same horizontal
        # but different vertical axis
        self.segments.append(new_segment)

if I don't repeat

self.x_cor = 0
self.y_cor = 0

in the 2nd method, I get feedback as "line 49, in create_segment new_segment.goto(self.x_cor, self.y_cor) AttributeError: 'Snake' object has no attribute 'x_cor'. Did you mean: 'y_cor'?

I really think repetition is not necessary but how do I avoid that? Thank you very much

CodePudding user response:

In the create_snake in your constructor you are basically trying to access self.x_cor before it is created, because create_snake calls create_segment that refers to self.x_cor before it is initialized.

  • Related