python - turtle 和 turtle 有什么区别?

标签 python python-2.7 turtle-graphics python-turtle

Python 2.7 版本中的 turtleTurtle 有何不同?

import turtle
star = turtle.Turtle()
for i in range(50):
    star.forward(50)
    star.right(144)
turtle.done()

最佳答案

turtle 模块很不寻常。为了让新手程序员更容易上手,Turtle 类的所有方法也可用作对默认(未命名)turtle 实例进行操作的顶级函数。 Screen 类的所有方法也可用作在默认(唯一)屏幕实例上运行的顶级函数。所以这两个:

import turtle

star = turtle.Turtle()  # turtle instance creation

for i in range(5):
    star.forward(50)  # turtle instance method
    star.right(144)  # turtle instance method

screen = turtle.Screen()  # access sole screen instance
screen.mainloop()  # screen instance method

还有这个:

import turtle

for i in range(5):
    turtle.forward(50)  # function, default turtle
    turtle.right(144)

turtle.done()  # function, mainloop() synonym, acts on singular screen instance

都是有效的实现。许多 turtle 程序最终将函数接口(interface)与对象接口(interface)混合在一起。为了避免这种情况,我强烈建议使用以下导入语法:

from turtle import Turtle, Screen

这会强制对象方法使用turtle,从而使函数方法不可用:

from turtle import Turtle, Screen

star = Turtle()  # turtle instance creation

for i in range(5):
    star.forward(50)  # turtle instance method
    star.right(144)  # turtle instance method

screen = Screen()  # access sole screen instance
screen.mainloop()  # screen instance method

关于python - turtle 和 turtle 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42874183/

相关文章:

python - Memcached:AWS Elasticache 上的自动发现 python 支持?

python - MultipleChoiceField 创建多个对象

python - 使用 Python 的 turtle 模块绘制此图案。一些正方形彼此重叠但倾斜有点像螺旋

python - turtle 图形无响应

python - NLTK 查找错误

c - 如何在 C 中实现适用于 turtle 图形的函数?

java和python在逻辑运算符中给出不同的结果

python - 具有特殊字符的 MYSQL::Connector/Python UPDATE 语句不起作用

python - Django 与 python 2.7 utf-8 问题

Django 虚拟主机设置。 Apache mod_wsgi