4

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

Alec
  • 6,521
  • 7
  • 23
  • 48

4 Answers4

8

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.

Alec
  • 6,521
  • 7
  • 23
  • 48
4

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.

Alec
  • 6,521
  • 7
  • 23
  • 48
rdas
  • 15,648
  • 5
  • 22
  • 36
  • 1
    If you're only importing turtle, this is a good solution, but for larger projects I'd advise against wildcard imports – Alec Apr 19 '19 at 19:48
  • If you import as x then you don't have to worry about namespace conflicts and you also don't have to append your imports whenever you use a new non-standard-library function. It's all personal preference, though. – Alec Apr 19 '19 at 19:52
  • @user1529891: Off the top of my head, plenty of people have ended up on Stack Overflow because they didn't realize that one of their wildcard imports introduced the wrong `open` function, or their `from numpy import *` hid the built-in `any` and `all`. Wildcard imports cause plenty of problems. – user2357112 supports Monica Apr 19 '19 at 20:58
4

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!

cdlane
  • 33,404
  • 4
  • 23
  • 63
2

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()
Alec
  • 6,521
  • 7
  • 23
  • 48
Mahmoud Elshahat
  • 1,678
  • 6
  • 15
  • 2
    You're not lazy enough :) `from turtle import forward as f, back as b, right as r, left as l, exitonclick`. – chepner Apr 19 '19 at 21:01
  • 1
    This highlights why turtle is dumb anyways. Just a bunch of repetitive actions. I like this solution because it makes these repetitive actions less cumbersome. – Alec Apr 19 '19 at 22:00
  • 1
    @alec_a, you are right, it's against what programing languages are for, which is Automating the boring stuff :) – Mahmoud Elshahat Apr 19 '19 at 22:10