简体   繁体   中英

Python : call a function with n arguments

I'm using a function from the drawSVG library: draw.Lines() . This function is drawing lines between specified points. For example, draw.Lines(x0, y0, x1, y1) draw a line between the point (x0, y0) and (x1, y1).

My problem is that I would like to draw n lines. To do that, I need to call the function with n arguments ( draw.Lines(x0, y1, ... xn, yn) ). Each argument must be a number. I have tried using lists and it didn't work.

I didn't find a way to do it properly. Right now I'm calling the function with the right amount of arguments but I do not like that.

For example if I want to print a square

svg.append(draw.Lines(10,10,100,10,100,100,10,100))

You can use argument unwrapping to pass a list to a function which expects multiple positional arguments.

Your example can be re-written as:

points = [10,10,100,10,100,100,10,100]
svg.append(draw.Lines(*points))

So if you have the end-coordinates for your n lines in a list, you can pass that (with a * ) to the drawLines function.

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