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 Answer 1

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.