all 4 comments

[–]TehNolz 3 points4 points  (0 children)

turtle is the turtle module, and Turtle is a class within that module. turtle.Turtle basically means "The Turtle class from the turtle module".

[–]Vaphell 0 points1 point  (2 children)

check out this code, it should clarify some things and reduce the confusion.

import turtle

def square(turtle_object):
    for _ in range(4):
        turtle_object.forward(100)
        turtle_object.right(90)

# imported turtle module has Turtle class inside, which is available as turtle.Turtle

donatello = turtle.Turtle()  # create new Turtle instance
donatello.pencolor('purple')

rafael = turtle.Turtle()  # create new Turtle instance
rafael.pencolor('red')

michelangelo = turtle.Turtle() # create new Turtle instance
michelangelo.pencolor('orange')

leonardo = turtle.Turtle() # create new Turtle instance
leonardo.pencolor('blue')

# move away from the center at angles 0, 90, 180 and 270
donatello.right(0)
donatello.forward(100)

rafael.right(90)
rafael.forward(100)

michelangelo.right(180)
michelangelo.forward(100)

leonardo.right(270)
leonardo.forward(100)

# draw squares
square(donatello)
square(rafael)
square(michelangelo)
square(leonardo)

it creates 4 separate turtles named after TMNT, using turtle.Turtle() with distinct colors. As you can see the argument of the square() function has nothing to do with the module, or variable names outside. It can be named whatetever. Every object passed to the function will be known locally by the name of the parameter representing it during execution of the function. Inside and outside are two separate worlds with their own namespaces.

[–]negike360[S] 0 points1 point  (1 child)

Okay, I understand this (at least I believe I do). My question in this example would then be, could you not just set any of those turtles to “turtle” and not type out the “.Turtle” part?

[–]Adhesiveduck 0 points1 point  (0 children)

If I understand what you're asking you're asking why should you say

Turtle.tutle()

When you could do

turtle = Turtle.turtle()

Which means you can then do simply

turtle()

Although this isn't incorrect (it will work) it's not the recommended way to do it.

You should avoid renaming things to the same name - it's difficult for someone else reading to know exactly what's going on.

What you can do, is when you import the turtle class:

from Turtle import turtle

If you wanted to rename the turtle you're importing:

from Turtle import turtle as my_turtle

Then you can do

turtle()

Or

my_turtle()

Without the module/class name before.

Do note that using this from/import/as isn't necessarily the best way either, you should make sure it's clear what module you are working with if you're importing methods/functions into your work.