Oliver Pickford
Oliver Pickford

Reputation: 13

How do I define a turtle in Python without creating a new one?

I am coding a game and am using turtles to import images as this is the only way I know, but I am having a problem where when I try to define my turtle outside of its original function (so that I can use it elsewhere), it creates another turtle with the same name rather than making the first turtle actually perform the 'goto' line. Here is an example of the problem that occurs in my code. (This is not from my actual code, however, the problem is the same and isn't affected by my other code:)

import turtle

def example():
    a = turtle.Turtle()

example()
a.goto(100,0)

This gives NameError: name 'a' is not defined. Then when I try to define a, like this:

example()
a = turtle.Turtle()
a.goto(100,0)

the output is of two turtles with only one of them performing the goto() command.

Thank you for taking your time to read my post, I am new to coding and this has been bothering me!

Upvotes: 0

Views: 1554

Answers (3)

cdlane
cdlane

Reputation: 41872

I read and write lots of Python turtle code and the most common ways I see this situation being handled are:

import turtle

def example():
    a.dot()  # do something with the turtle

a = turtle.Turtle()

example()

a.goto(100, 0)

Or, just as often:

import turtle

def example(t):
    t.dot()  # do something with the turtle

a = turtle.Turtle()

example(a)

a.goto(100, 0)

These approaches also reflect that turtles always behave like global entities (they're registered on a list inside the turtle library so are never garbage collected.) Creating one inside a function, and not returning it, doesn't make it local to the function -- just the pointer to it is local. The turtle continues to exist nearly inaccessible afterward.

Upvotes: 0

nfmcclure
nfmcclure

Reputation: 3141

I think you're very close. The one thing I would do is to make sure your python function example() returns an object. Right now it returns nothing. Also, be sure to assign whatever the function returns to a variable. I would try:

import turtle

def example():
    a_turtle = turtle.Turtle()
    return a_turtle

a = example()
a.goto(100,0)
b = example()
b.goto(50,0)

I don't know the turtle package too well, so I'm not sure if there is a way to get multiple turtles to respond with one command, but the above code works for me to get two turtles, a and b to move.

Edit: Also, like other answers say, read up a bit on namespaces and functions. It'll help to make clear what is accessible where.

Upvotes: 1

Johan
Johan

Reputation: 3611

It happens because variables inside a function is bound to the functions scope. Basically, if you define the variable a inside the function example(), the variable only lives inside that function and will die when the function call ends.

If you for instance try and run the following:

def foo():
    bar = 2

foo()
print bar

Output

> NameError: name 'bar' is not defined

This happens because bar only lives inside foo. You can however use use the global statement to bind bar to the global scope.

def foo():
    global bar
    bar = 2

foo()
print bar

Output:

> 2

This is however not a very good practice. Instead, in your case you could return the object when calling example and thus get the object you've created.

import turtle

def example():
    a = turtle.Turtle()
    return a

a = example()
a.goto(100,0)

Upvotes: 2

Related Questions