3

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()
dspencer
  • 3,649
  • 4
  • 15
  • 35
Jdpurohit
  • 61
  • 5
  • turtle is a module that contains the class Turtle. If you are using Debian OS check in your default python libs folders. . e.g /usr/libs/python (version)/turtle – repzero Mar 18 '17 at 12:09

6 Answers6

4

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

and this:

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
cdlane
  • 33,404
  • 4
  • 23
  • 63
1

turtle is the module that you import while Turtle is that name of the class. Using from turtle import * removes the need for turtle.Turtle.

atomix
  • 11
  • 3
1

Simply put, turtle is the package or library and Turtle() class constructor method used to instantiate the class.

ChadSixt
  • 21
  • 3
0

turtle is the name of the package while Turtle is the name of the class.

An alternate way of importing the module would be:

import turtle.Turtle

Also, are you sure the last line is turtle.done() and not star.done()?

Prav
  • 343
  • 1
  • 2
  • 11
  • 1
    Or `from turtle import Turtle` but you will have to replace `turtle.Turtle` with just `Turtle` everywhere in the current module – Andrew Che Mar 18 '17 at 15:55
  • 1
    `star.done()` would be an error, `turtle.done()` is valid. The `done()` top level function is a synonym of the `mainloop()` top level function and doesn't apply to a turtle instance. As a top level function ,`mainloop()` calls the same named method of the singular screen instance. – cdlane Mar 18 '17 at 16:06
  • 1
    I am sure about turtle.done() as done() comes under `_tg_utilities` – Jdpurohit Mar 18 '17 at 18:35
0

The first turtle is called turtle and is referenced by it's name or it in a variable, the turtle.Turtle method creates a new turtle and (most of the time), you set it to a variable.

Psychzander
  • 83
  • 11
0

turtle is a method which contains a class which is noble Turtle