Home > OS >  Is there a difference in python turtle speed?
Is there a difference in python turtle speed?

Time:12-08

In korean python textbook for highschool

to use turtle lib with the fastest pace user can use two verson of command.

import turtle as t

t.speed(0)
t.speed=0

is there a difference with two command?

I try to ask my teacher because of performance evaluation but.. unfortunately she didnt knowㅜㅠ..

CodePudding user response:

t.speed = 0 overwrites the module function (or Turtle() method if you'd made an instance) with an integer 0:

>>> import turtle
>>> turtle.speed = 0
>>> turtle.speed("fastest")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable

In general, all turtle API setters are function/method calls, not assignments. For example, turtle.setheading(90) and turtle.pencolor("red") set the heading to 90 degrees and the pen color to red, respectively.

If you want the turtle speed to be instantaneous, try turtle.tracer(0) and run turtle.update() to trigger a rerender after you've done your drawing.

As an aside, please don't use import turtle as t (although that's better than from turtle import *). It's unclear that t is the module and not a turtle instance, which makes ownership of the module confusing. Prefer making an instance of the turtle rather than calling global turtle functions, for example, t = turtle.Turtle().

CodePudding user response:

t.speed=0 doesn't actually work (as ggorlen pointed out). If you want your trutle drawing to go really fast, you can make them happend off screen and only show the result at the end using screen(), tracer() and update():

Try this without the extra lines, then again after uncommenting the 3 lines:

import turtle as tg

def spiral(N):
    tg.clear()
    d = 1
    for i in range(N):
        tg.forward(d)
        tg.left(20)
        d =0.3

## wn = tg.Screen() # Remove the 3 comments to make it fast
## wn.tracer(0)

tg.speed(0)
spiral(1000)

## wn.update()
  • Related