0

We're trying to make the turtle color be coral and came across this.

from turtle import Turtle, Screen

# case 1: assign variable
foo = Turtle()
foo.shape("turtle")
foo.color("coral")
Screen().exitonclick()

# case 2: use it w/o assign
Turtle().shape("turtle")
Turtle().color("coral")
Screen().exitonclick()

# case 3: methods directly in the module
from turtle import *

shape("turtle")
color("coral")
exitonclick()

case 1 = case 3

case 2

In case 2 the color() method only fills part of the turtle.

In a more general scope,

class Object():

    def __init__(self):
        print("object created")

    def introduce(self):
        print("I am an object")

    def sleep(self):
        print("I am sleeping")


# case 1
foo = Object()
foo.introduce()

# case 2
Object().introduce()

Here, case 1 & 2 are the same.

1 Answers1

1

In case 1, you're modifying a unique turtle instance that you created:

# case 1: assign variable
foo = Turtle()
foo.shape("turtle")
foo.color("coral")

In case 2, you are creating a unique turtle instance, and changing its shape followed by the creation of a second unique turtle instance and changing it's color. I.e. these are two different turtles and you've lost your handle to further control them:

# case 2: use it w/o assign
Turtle().shape("turtle")
Turtle().color("coral")

In case 3, you are changing the shape and color of the default turtle instance that is provided by the library for simple programs:

# case 3: methods directly in the module
from turtle import *

shape("turtle")
color("coral")
cdlane
  • 40,441
  • 5
  • 32
  • 81