JAEU
JAEU

Reputation: 46

AttirbuteError in class(turtle class inheritance)

class Turtle_new(turtle.Turtle):
  def __init__(self):
    self.walks = 10000

  def go(self):
    r1 = random.randint(0,90)
    r2 = random.randint(270,360)
    if random.randint(0,1): 
      self.setheading(r1)
    else:
      self.setheading(r2)
    self.pensize(3)
    self.speed('fast')
    r4 = random.randint(0, 255)

self.setheading(r1).

The Attirbute Error occurs in line 9.

'Turtle_new' object has no attribute '_orient'

This is the error. I have changed "self.setheading" to "super()" but the error occurs. Also same error occurs in self.speed('fast) changed "_orient" to "_screen". What is the problem?

Upvotes: 0

Views: 28

Answers (1)

chepner
chepner

Reputation: 532093

You have to ensure Turtle.__init__ is called so that your instance is initialized properly. Unlike some languages, the parent initializer is not called automatically; you have to be explicit.

from turtle import Turtle


class Turtle_new(Turtle):
    def __init__(self, kwargs):
         super().__init__(**kwargs)
         self.walks = 10000

    ...

Turtle.__init__ (indirectly) initializes self._orient. Exactly how isn't really relevant to this answer; you can explore the turtle module if you are curious.

Upvotes: 1

Related Questions