F
F
fantom_ask2020-04-24 09:12:41
Python
fantom_ask, 2020-04-24 09:12:41

How to work with a function with a large number of optional parameters?

I have a function with a lot of optional parameters
example

def fun(name="name", time=0, size=300, color="#fff"):

And in order to change the color parameter, I need to write . Is it possible to shorten this somehow? the only thing I came up with is to use an associative array
fun("name", 0, 300, "#ff0"):


def fun(arr):
  name =  ["name", "time", "size," "color"]
  val = ["name", 0 , 300 , "#fff" ]
  num = 0
  for key in name :
    try: 
      arr[key]
    except KeyError:
      arr[key] = val [num ]

Arr = {
"color"="#ff0"
}
fun(Arr)


But is there a better way to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-04-24
@fantom_ask

def fun(**kwargs):
    color = kwargs.get('color')

PS well and
def fun(name="name", time=0, size=300, color="#fff"):

simple enough
fun(color="#ff0")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question