Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading