ishaishai
ishaishai

Reputation: 73

Python dictionary pass argument into func used as value

I'm trying to use a dictionary by passing arguments to a function that is used by a key lets say for example:

def myfunc(myval):
    print(myval)


mydict = {'key1':myfunc(a)}

mydict['key1'] = myfunc(2)

line 7, in mydict = {'key1':myfunc(a)} NameError: name 'a' is not defined

how its possible to be made?

Upvotes: 1

Views: 37

Answers (1)

Shivam Singh
Shivam Singh

Reputation: 1624

mydict = {'key1':myfunc}
mydict['key1'](2) # or any other argument

So just define the function inside the dictionary

Upvotes: 1

Related Questions