Baraa najjar
Baraa najjar

Reputation: 71

Is there a way to shorten my code so that I don't have to write "turtle." every time I use functions from the turtle module?

I am trying to draw squares using turtle in python, and every time I want to command it to do something I must write turtle.

import turtle


turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.back(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.back(100)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.back(200)
turtle.right(90)
turtle.forward(100)
turtle.right(90)
turtle.back(100)



turtle.exitonclick()

I expect to write my code without having to write turtle every time

Upvotes: 4

Views: 1135

Answers (4)

Alec
Alec

Reputation: 9536

You can import everything from the turtle module by writing

from turtle import *  # this is a wildcard import

Instead, however, you should just import turtle as t (or whatever else you want), like so:

import turtle as t  # you can replace t with any valid variable name

Because wildcard imports tend to create function definition conflictions

Conversely, you could import only the classes (or methods) you need from from the module. Turtle is a necessary import:

from turtle import Turtle

Now, we have to instantiate it:

t = Turtle()

Now we can use it:

t.do_something()  # methods that act on the turtle, like forward and backward

That will not import the Screen module however, so you won't be able to use exitonclick() unless you import Screen too:

from turtle import Turtle, Screen
s = Screen()
s.exitonclick()

As @cdlane notes though, loops may actually be your best bet for reducing the amount of code you have to write. This code:

for _ in range(x):
    turtle.forward(100)
    turtle.right(90)

Moves the turtle forwards then rightwards x times.

Upvotes: 8

rdas
rdas

Reputation: 21275

You could use the wildcard import:

from turtle import * 

But it'd be better to use the prefixed imports to keep your namespaces clean. See @alec_a's answer.

Upvotes: 4

Mahmoud Elshahat
Mahmoud Elshahat

Reputation: 1959

Disclaimer: this answer is for lazy people like me :)

There are already good answers that show you how to solve your problem, and that warn you regarding wildcard imports.

If you just want to play with turtle module you can make your life easy, i.e. instead of writing turtle.forward(90) it is better to just write forward(90) but it will be super easy if you just write f(90)

again it will affect the readability of your code but common it deserve a trial

now your code will looks like

Edit: modify imports in one line as suggested by @chepner to be super lazier

from turtle import forward as f, back as b, right as r, left as l, exitonclick

# to draw one square
f(100)
r(90)
f(100)
r(90)
f(100)
r(90)
f(100)

exitonclick()

Upvotes: 2

cdlane
cdlane

Reputation: 41872

Do you have any idea how many comments there are on SO saying "Don't use wildcard imports" in response to people doing from turtle import * as folks are suggesting? I'll further argue, don't do import turtle as t as it exposes the functional interface to turtle. The turtle module is object-oriented, you need only expose that interface. If you're tired of typing so much, learn about loops:

from turtle import Screen, Turtle

t = Turtle()

for _ in range(4):
    t.forward(100)
    t.right(90)

for _ in range(4):
    t.backward(100)
    t.left(90)

t.backward(100)

for _ in range(3):
    t.backward(100)
    t.left(90)

s = Screen()

s.exitonclick()

Admittedly, I don't really mind wildcard imports for short turtle & tkinter examples as well as Zelle graphics programs. But none of that fd() nonsense instead of forward() either! Celebrate being a turtle, don't hide in your shell!

Upvotes: 4

Related Questions