python-3.5 - Python:如何在函数内部显式传递函数
问题描述
所以我知道可以将函数传递给函数。但我正在使用某个库,它有一个成员变量和一个同名的函数。如何告诉python在函数内部传递函数而不是成员变量?
以下示例代码显示 python 会错误地认为我正在传递成员变量:
class foo():
def __init__(self):
self.ambiguous = "member variable" # Member variable I don't want to be passed into another function
def ambiguous(self): # Function I want to be passed into another function
print("function")
def testFunc(func): # This function is intended to be passed a function
func()
def main():
myFoo = foo()
testFunc(myFoo.ambiguous) # this will pass the member variable (I want to pass the function)
if __name__ == "__main__":
main()
我正在使用 python 3.5
解决方案
推荐阅读
- django - Postgres 的 Django 数据库模型创建和索引
- javascript - 如何将项目从一个列表拖放到另一个列表而不从第一个列表中删除?
- mysql - 将 mysql 查询转换为 laravel 查询生成器
- angular - Angular 发布请求发送 [object Object]
- mongodb - 如何在 $set 数据部分 MongoDB 中使用动态内容
- html - 如何处理 html5 图片标签中的图片未找到错误
- r - R:查找一个变量包含另一个变量的值
- r - r - ggord NMDS 图按组的点状和实心椭圆
- gnome-shell-extensions - GJS:global.window_manager.get_workspaces() 未定义
- python - 如何创建具有周范围的系列?