Python Forum
What does turtle.Turtle() actually do?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What does turtle.Turtle() actually do?
#1
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.
Reply
#2
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".
karabakh likes this post
Reply
#3
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.
karabakh likes this post
Reply
#4
(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.
Reply
#5
(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.
Reply
#6
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.
karabakh likes this post
Reply
#7
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().
Reply
#8
(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()
karabakh likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo Problem installing turtle MasterJediKnight7 17 24,304 Mar-18-2024, 10:22 AM
Last Post: bmohamadyar313
  Trying to make a board with turtle, nothing happens when running script Quascia 3 607 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 902 Oct-25-2023, 09:09 AM
Last Post: codelab
  Turtle Detection ChinaPlaneFlyer 1 624 Apr-17-2023, 02:49 PM
Last Post: deanhystad
  Turtle Graphics - Screen & Window Sizing jerryf 1 772 Feb-09-2023, 08:02 PM
Last Post: jerryf
  Turtle.setpos() vs text position on screen query ElectronWrangler 0 1,508 Nov-26-2022, 02:39 AM
Last Post: ElectronWrangler
  Turtle onkey() simonc8 12 7,286 Jun-29-2021, 09:09 AM
Last Post: simonc8
Smile PROJECTILE WITH TURTLE artnote1337 2 2,648 Dec-26-2020, 03:39 PM
Last Post: MK_CodingSpace
  Is there a way to move text created by turtle.write()? derekered 7 5,821 Dec-16-2020, 09:44 AM
Last Post: itexamples
  SOLVED - Collision detection - TURTLE OuateDePhoque 9 11,174 Nov-18-2020, 06:29 PM
Last Post: OuateDePhoque

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020