乌龟和乌龟的区别? [英] Difference between turtle and Turtle?

查看:51
本文介绍了乌龟和乌龟的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How are turtle and Turtle different from each other in python version 2.7?

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

推荐答案

turtle 模块异常.为了让初学者更容易,Turtle 类的所有方法也可用作对默认(未命名)海龟实例进行操作的顶级函数.Screen 类的所有方法也可用作对默认(唯一)屏幕实例进行操作的顶级函数.所以这两个:

The turtle module is unusual. To make it easier for beginning programmers, all methods of the Turtle class are also available as top level functions that operate on the default (unnamed) turtle instance. All methods of the Screen class are also available as top level functions that operate on the default (sole) screen instance. So both this:

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

都是有效的实现.许多海龟程序最终将功能接口与对象接口混合在一起.为了避免这种情况,我强烈推荐以下导入语法:

are both valid implementations. Many turtle programs end up mixing the functional interface with the object interface. To avoid this, I strongly recommend the following import syntax:

from turtle import Turtle, Screen

这迫使对象方法使用乌龟,使得函数方法不可用:

This forces the object approach to using turtle, making the functional approach unavailable:

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

这篇关于乌龟和乌龟的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆