Python Forum

Full Version: What does turtle.Turtle() actually do?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What does turtle.Turtle() actually do?

I am trying to follow this tutorial on youtube:
https://www.youtube.com/watch?v=crV6T3piwHQ

You can see that the author first generates the screen with turtle.Screen(). Only after doing that does he call turtle.Turtle() to use the rest of functionalities of that module. In some other modules I have come across with, we always call the name.Name() first in order to have access to all functionalities listed in dir(module). But in this case, it wasn't the first thing that was called. That made me question, what does that line actually do? Not only in turtle module, but generally - when we call module.Module(). If this is too fundamental of a question, what is it that I am not understanding correctly? Is it python syntax? or is it more module specific?

Thank you.
Never used Turtle, but I think turtle.Turtle() is "turtle" is the module, "Turtle" is the Turtle class in the module... aka your player or some other moveable object.

So in a general sense when you call a class like:
player = turtle.Turtle()  #don't know exact syntax
You are creating a Turtle object named "player".
The naming conventions in PEP 8 should help you here. You know that the dot syntax means to access the thing after the dot in the module before the dot. The thing after the dot Turtle(), looks a bit like a function call. However, given the naming conventions, functions begin with lowercase letters and classes begin with uppercase ones. Therefore, Turtle is a class whose constructor is being called.
(Dec-11-2020, 03:41 PM)michael1789 Wrote: [ -> ]Never used Turtle, but I think turtle.Turtle() is "turtle" is the module, "Turtle" is the Turtle class in the module... aka your player or some other moveable object.

So in a general sense when you call a class like:
player = turtle.Turtle()  #don't know exact syntax
You are creating a Turtle object named "player".



That makes sense that we are creating an object using Turtle() class. However, when I review the documentation here:
https://docs.python.org/3/library/turtle...0with%20it.
We can see that the first example draws a star using :
from turtle import *
color('red', 'yellow')
begin_fill()
while True:
    forward(200)
    left(170)
    if abs(pos()) < 1:
        break
end_fill()
done()
We did not create an object here - we just used the methods within turtle. I understand this might be a very fundamental question, but how do I know in which cases do I need to invoke Turtle() class in order to generate something on the screen? And in which cases there's no need and I can just go straight ahead and use the methods that are not necessarily within some class?

Appreciate all the help I can get. Thank you.
(Dec-11-2020, 03:52 PM)ndc85430 Wrote: [ -> ]The naming conventions in PEP 8 should help you here. You know that the dot syntax means to access the thing after the dot in the module before the dot. The thing after the dot Turtle(), looks a bit like a function call. However, given the naming conventions, functions begin with lowercase letters and classes begin with uppercase ones. Therefore, Turtle is a class whose constructor is being called.

I think this is something that I need to read. I always confuse when to call classes, when to use parentheses, etc etc.

Thank you for pointing this out.
The turtle module is a bit special. When you use the module, you get your first turtle for "free" (called the anoymous turtle). But you can also invoke new turtles and have them around as well.

player = Turtle.turtle() creates a new turtle on the canvas and assigns it to "player".

Almost any other module would make you do the equivalent to create the objects you use. But probably to make simple turtle scripts easier, you don't have to do that. You can just start using the unnamed turtle without instantiation. Don't think of that as a rule for how python programs and modules should work.
You use turtle.Turtle() to create a pen for you to draw. Without it, you don't have a pen. It is euqivalent to turtle.Pen().
(Dec-11-2020, 06:28 PM)MK_CodingSpace Wrote: [ -> ]You use turtle.Turtle() to create a pen for you to draw. Without it, you don't have a pen.

I think the confusion was that you do have a pen without the call. One is automatically created. You can name it after the fact via turtle.getpen()