python function *args and **kwargs with other specified keyword arguments

前端 未结 3 621
别那么骄傲
别那么骄傲 2021-02-07 15:31

I have a Python class with a method which should accept arguments and keyword arguments this way

class plot:
    def __init__(self, x, y):
        self.x = x
            


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-07 16:28

    You would use a different pattern:

    def set_axis(self, *args, **kwargs):
        xlabel = kwargs.get('xlabel', 'x')
        ylabel = kwargs.get('ylabel', 'y')
    

    This allows you to use * and ** while keeping the fallback values if keyword arguments aren't defined.

提交回复
热议问题