简体   繁体   中英

How to run functions in forward order from lambda functions (Python)

I'm trying to convert the function below to the lambda function. And this function below has some functions that need to be executed in order. I searched many times, but there aren't have any clues. What I trying to convert is

def drawSquare(moveLength):
    A_Turtle.right(90)
    moveAndturn(moveLength)
    for n in range(6):                     
        A_Turtle.forward(moveLength)
        A_Turtle.left(90)

and this is what I do so far.

moveAndturn = lambda moveLength : [A_Turtle.forward(moveLength) A_Turtle.left(90) for n in range(6)]

Before, A Disclaimer

This use of list comprehension in Python is problematic because it can hide the semantics of the code and I would advice you not to use it in this case. Your code is fine as is, but you can use lambda and list comprehensions to do the same.

draw_square = lambda moveLength : [
                    A_Turtle.right(moveLength), 
                    moveAndturn(moveLength), 
                    [
                        [A_Turtle.forward(moveLength), A_Turtle.left(moveLength)] for i in range(4)]
                    ]

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