Python字典将参数传递到函数中用作值

标签 python

我试图通过将参数传递给键使用的函数来使用字典,例如:

def myfunc(myval):
    print(myval)


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

mydict['key1'] = myfunc(2)

第 7 行,在 mydict = {'key1':myfunc(a)} NameError:名称“a”未定义

它是如何制作的?

最佳答案

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

所以只需在字典中定义函数

关于Python字典将参数传递到函数中用作值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50130712/

相关文章:

python - 通过 Python 与 InfluxDB 了解 Unix 纪元时间的差异

python - 将 PySpark 数据框列从列表转换为字符串

python - Python中简单的不确定进度条

python - 检索调用方法并即时构建调用?

python - 如何获取 sqlalchemy 中受影响的行数?

python - IPython %run magic -n 开关不工作

python - 在 Wing101 中切换到 Python 3.6

python - 使用pyodbc的sql server返回值

python - 'reticulate' r-package 导入 python 脚本时出错

python - 通过复制记录创建大型数据集