-2

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)
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Well, does your code throw an error? – timgeb Jun 15 '22 at 12:09
  • Traceback (most recent call last): File "", line 1, in NameError: name 'p2' is not defined – Karen William Jun 15 '22 at 12:09
  • Hi Karen, dont be sorry for asking questions, its what SO is for. That being said what are you trying to do? why not define `p` in the function. You need to explain what you are trying to do, what you expect output to be, and what the problem is. – Craicerjack Jun 15 '22 at 12:11

1 Answers1

1

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
timgeb
  • 76,762
  • 20
  • 123
  • 145