python / python/cpython

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

未关闭
#126,399 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先从 turtle.py 中 clone()、clear() 和 forward() 的定义开始,然后复现 issue 的 t1/t2 示例,并查看相关 PR gh-126401 和 gh-127189。完成的标准是:克隆出的 Turtle 能跟踪并清除自己的绘图,而不会清除原始 Turtle 的轨迹。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
computer-graphics
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。