creating default parameters in python -
i have function 3 parameters, parameters 2 , 3 have default values. default value parameter 3 dependent on properties of parameter one. however, cannot perform operations on parameter 1 in 'def' line i'm having trouble finding efficient way of defining default value of parameter three. standard way in python?
the following code i'm working with. line 1 throws error because haven't defined 'data' yet. best workaround?
def stooge_sort(data, = 0, b = len(data) - 1): if data[a] > data[b]: data[a], data[b] = data[b], data[a] if b + 1 - >= 3: thirds = (b + 1 - a) / 3 stooge_sort(data, a, b - thirds) stooge_sort(data, + thirds, b) stooge_sort(data, a, b - thirds) return data
remember default arguments evaluated once when function object created, there's no way put expression in there. same reason people surprised behaviour of default arguments assigned mutable values.
the usual way pick default value know b
never be. none
choice.
def stooge_sort(data, a=0, b=none): if b none: b = len(data) - 1
Comments
Post a Comment