How can I define a function with default parameter values and optional parameters in Python?

前端 未结 4 1524
别跟我提以往
别跟我提以往 2021-01-14 23:29

I want to define a function to which the input parameters can be omitted or have a default value.

I have this function:

def nearxy(x,y,x0,y0,z):
   d         


        
4条回答
  •  梦毁少年i
    2021-01-14 23:43

    give default values to x0,y0 like this and if z is optional also :

    def nearxy(x,y,x0=0,y0=0,z=None):
       distance=[]
       for i in range(0,len(x)):   
       distance.append(abs(math.sqrt((x[i]-x0)**2+(y[i]-y0)**2)))
       if z is not None:
            blah blah
       return min(distance)
    

    call :

    nearxy(1,2)
    

    if you want only toassign z :

     nearxy(1,2,z=3)
    

    ....

    hope this helps

提交回复
热议问题