Using dict with parameter

Is it possible to use a function with a dict argument which has other argument like p2 in it?

def f(l, p2, p = {"t": p2}):
    print(l)

f(12, 13)

>Solution :

No. The dict {"t": p2} is created at function definition, at which time p2 is not bound to anything.

What you can do is

def f(l, p2, p=None):
    if p is None:
        p = {'t': p2}

    # rest of code

Leave a Reply