简体   繁体   中英

I'm having some trouble with python turtle

So, I'm pretty new with turtle, and I was going to make some text, but when I use this function;

    turto1=turtle.Turtle
    turto2=turtle.Turtle
    def spuper():
        turto1.penup()
        turto2.penup()
        turto1.goto(-150,40)
        turto2.goto(-130,40)

I get this error:

    TypeError: penup() missing 1 required 
    positional argument: 'self'

I'm not sure why this happens, and I'm pretty sure the penup() command doesn't have any arguments. Does anyone know what I did wrong?

Change both turtle.Turtle to turtle.Turtle() . Without the () you're assigning the class itself to the variable. Meaning that when you try to call methods on it, the first argument, an instance of the class, won't be implicitly passed to the method. This means that you'll either have to explicitly pass in an instance ( turtle.Turtle.penup(aTurtleInstanceThatYouDefinedElsewhere) ), or the method call will be treated as a static method, which will cause the error to be thrown if it is not defined as a static method. With the () you're creating an instance of the class and assigning it to the variable. Meaning that when you call a method on it, you will implicitly pass the instance itself to the function as the first argument.

That's the one argument that turto1.penup() is looking for. The instance that it's being called on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM