繁体   English   中英

如何在python中传递变量名而不是方法名

[英]How to pass variable name instead of method name in python

我正在使用Google Places API代码获取位置及其详细信息,因此在该代码中,我要传递变量名称而不是其属性类型

这是片段

工作正常

query_result = google_places.nearby_search(
        location='London, England', keyword='Fish and Chips',
        radius=20000, types=[types.TYPE_FOOD])

给出错误

place_type='TYPE_FOOD'
query_result = google_places.nearby_search(
            location='London, England', keyword='Fish and Chips',
            radius=20000, types=[types.place_type])

AttributeError: module 'googleplaces.types' has no attribute 'place_type'

关于如何完成的任何建议?

谢谢

多姆尼克

您可以使用getattr

1)如果TYPE_FOOD是属性,则可以使用:

getattr(types, place_type)

2)如果TYPE_FOOD是一种方法,则应调用它:

getattr(types, place_type)()

注意:我假设您的方法不带参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM