-1
def func(a,  b,  c,  config)

I will always need to choose one of the three(a, b, c) and the config is always required. It should work like:

func(a="hello", config) 

and

func(b="hello", config) 

Is possible to place optional parameters before the required ones?

  • It aready has been answered [Here](https://stackoverflow.com/questions/9539921/how-do-i-create-a-python-function-with-optional-arguments). – thegamer1070 May 26 '18 at 08:49
  • 2
    Possible duplicate of [How do I create a Python function with optional arguments?](https://stackoverflow.com/questions/9539921/how-do-i-create-a-python-function-with-optional-arguments) – Netwave May 26 '18 at 08:51

1 Answers1

2

Make use of default parameters.

def func(config, a = None, b = None, c = None):
    pass
func(config, b="B")

Use dictionary

params = {"config": myConfig, "b": valB}  # you want b here
def func(**params):
    config = params["config"]
    a = params.get("a")
    b = params.get("b")
    c = params.get("c")
func(params)

Edit: If you want config should always be the last, use it as

def func(a=None, b=None, c=None, config=None):
    pass
func(b=valB, config=myConfig)
Arpit Kathuria
  • 414
  • 4
  • 13