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()
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
Simply put, turtle is the package or library and Turtle() class constructor method used to instantiate the class.
hope you have a good time!
"turtle" is actually the module that we use .
Turtle or we can say as turtle.Turtle is actually a object that you can move it change speed, color, shape and ....
here you can see an example:
enemy = turtle.Turtle(shape="rabbit.gif")
enemy.penup()
enemy.speed(0)
enemy.setposition(400, -180)
or another one:
player = turtle.Turtle(shape="turtle.gif")
player.penup()
player.speed(0)
player.left(90)
player.setposition(-400, -200)
or:
border = turtle.Turtle()
border.speed(0)
border.color("white")
border.penup()
border.setposition(-300, -300)
border.pendown()
border.pensize(3)
border.hideturtle()
hope it helps!
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.
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()?
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.
turtle is a method which contains a class which is noble Turtle
Related
I'm using the turtle module. My goal is to keep the window open, and I have heard to use turtle.done() to do so. So far, I've always got this error message:
AttributeError: type object 'Turtle' has no attribute 'done'. Did you mean: 'clone'?
Here is my code.
import turtle
from turtle import Turtle
turtle = Turtle()
turtle.forward(100)
turtle.done()
You shouldn't have a variable name the same as a module. Change the object name from turtle to t, for example, and it'll work:
import turtle
t = turtle.Turtle()
t.forward(100)
turtle.done()
done is a function in the turtle module.
By writing turtle = Turtle() you overwrote the name turtle so that it no longer refers to the module, but to an instance of the class Turtle which does in fact not have a method called done.
The simplest solution would be to choose a different variable name for the Turtle instance.
done is a module-level function, not a method on the Turtle object. Import it from the turtle module on the same line where you import the Turtle class:
from turtle import Turtle, done
turtle = Turtle()
turtle.forward(100)
done()
or import the entire module as turtle (in which case you should not reuse the name turtle for one of your own variables):
import turtle
t = turtle.Turtle()
t.forward(100)
turtle.done()
The issue begins when I try to link my modules/files, the turtle class does not instantiate objects to their proper values. I tried changing paths for my python interpreter, but that didn't work. The funny thing is, though- the turtle class works fine with only one module, meaning the object from the class Turtle receives the correct data I give to instantiate it: shape, size, color, etc.
# player.py
from turtle import Turtle
class Player(Turtle):
def __int__(self):
super().__init__()
self.shape('turtle')
screen.exitonclick()
#***********************
# main.py
from player import Player
from turtle import Screen
screen = Screen()
player = Player()
screen.exitonclick()
from turtle import Turtle, Screen, TurtleScreen
turtle = Turtle()
screen = Screen()
print(isinstance(turtle, Turtle))
print(isinstance(screen, TurtleScreen))
print(issubclass(Screen,TurtleScreen))
print(isinstance(screen, Screen))
The last 2 prints got some error, debugging says "Screen" is not a class. But from documentation it says "Screen is a subclass of TurtleScreen". So what's wrong here? Is it related to something called Singolton object?
There is no class called Screen. There is a (standalone turtle) function called Screen that returns the singleton TurtleScreen instance. So this is correct:
screen = Screen()
print(isinstance(screen, TurtleScreen))
(The hidden reality is that it is an instance of class _Screen which is a subclass of class TurtleScreen which is a subclass of TurtleScreenBase)
For simplicity, we pretend that we are getting an instance of class Screen that we invoke methods on:
from turtle import Screen
screen = Screen()
screen.setup(800, 600)
But that's a convenient fiction.
I am learning turtle graphics in python and for some reason there is a second turtle on the screen and I haven't even created a second turtle. How can I get rid of the second turtle?
import turtle
s = turtle.getscreen()
t = turtle.Turtle()
for i in range(4):
t.fd(100)
t.rt(90)
turtle.exitonclick()
The second turtle at the starting location appears because of the line s = turtle.getscreen().
This line is not needed (you do not use s), and if you remove it this turtle disappears but the rest of the code seems to work as before.
The turtle library exposes two interfaces, a functional one (for beginners) and an object-oriented one. You got that extra turtle because you mixed the two interfaces (and #mkrieger1's solution doesn't fix that completely).
I always recommend an import like:
from turtle import Screen, Turtle
screen = Screen()
turtle = Turtle()
for _ in range(4):
turtle.forward(100)
turtle.right(90)
screen.exitonclick()
This gives you access to the object-oriented interface and blocks the functional one. Mixing the two leads to all sorts of bugs and artifacts.
To combine the answer from mkrieger1 and cdlane, you could replace
s = turtle.getscreen()
with
s = turtle.Screen()
You've still got a variable holding the screen (in case you should ever need it), and it doesn't generate that extra turtle in the center.
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()
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
Simply put, turtle is the package or library and Turtle() class constructor method used to instantiate the class.
hope you have a good time!
"turtle" is actually the module that we use .
Turtle or we can say as turtle.Turtle is actually a object that you can move it change speed, color, shape and ....
here you can see an example:
enemy = turtle.Turtle(shape="rabbit.gif")
enemy.penup()
enemy.speed(0)
enemy.setposition(400, -180)
or another one:
player = turtle.Turtle(shape="turtle.gif")
player.penup()
player.speed(0)
player.left(90)
player.setposition(-400, -200)
or:
border = turtle.Turtle()
border.speed(0)
border.color("white")
border.penup()
border.setposition(-300, -300)
border.pendown()
border.pensize(3)
border.hideturtle()
hope it helps!
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.
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()?
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.
turtle is a method which contains a class which is noble Turtle