python / python/cpython

Unexpected behavior of clear() after clone() in turtle.py module

Open
#126,399 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Unexpected behavior report

Unexpected behavior description:

Hello, I recently started teaching Python. I found turtle module interesting and I'm planning to use the module for deeper understanding of loop, condition, and function. I tried to simplify some turtle examples, just to show how for loop works. I cloned Turtle object, moved the clones to initial position, and cleared all Turtles before start drawing. However, clear() method did not work as I expected. Here is the code that I used.

from turtle import *

t1 = Turtle()
t2 = t1.clone()

t1.forward(100)
t2.right(90)
t2.forward(100)

t2.clear()

I expected t2.clear() to clear the trace of the cloned Turtle. Instead, t2.clear() erased the trace of the original Turtle, where the trace was drawn after the cloning.

I wanted to know what is going on, so I read the definition of clear() method, and the method seemed to delete all items in self.items. Also, the source code of forward() used self.currentLineItem when drawing. So I tracked items and currentLineItem in my code. Here are the results of the tracking.

from turtle import *

#t1.currentLineItem, t1.items, t2.currentLineItem, t2.items

t1 = Turtle()   #1, [1], None, None

t2 = t1.clone() #2, [1,2], 3, [1,2]

t1.forward(100) #2, [1,2], 3, [1,2]
t2.right(90)    #2, [1,2], 3, [1,2]
t2.forward(100) #2, [1,2], 3, [1,2]

t2.clear()      #2, [1,2], 4, [4]

So, t2 drew lines in 3, however, because 3 is not in t2.items, t2.clear() does not delete 3. Also, t1 drew lines in 2, and, because 2 is in t2.items, t2.clear() delete 2.

I think that the items of the cloned Turtle should be [1,3] instead of [1,2] after clone() is called.

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs
  • gh-126401
  • gh-127189

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the turtle.py definitions of clone(), clear(), and forward(), then reproduce the issue's t1/t2 example and review linked PRs gh-126401 and gh-127189. Done means a cloned Turtle tracks and clears its own drawing without clearing the original Turtle's trace.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
computer-graphics
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.