【问题标题】:How to pass variable name instead of method name in python如何在python中传递变量名而不是方法名
【发布时间】:2017-11-30 11:07:00
【问题描述】:

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

这里是sn-p

工作正常

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'

有什么建议吗?

谢谢

多尼克

【问题讨论】:

标签: python google-places-api variable-assignment


【解决方案1】:

您可以使用getattr

1) 如果 TYPE_FOOD 是您可以使用的属性:

getattr(types, place_type)

2) 如果 TYPE_FOOD 是一个方法,你应该调用它:

getattr(types, place_type)()

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

【讨论】:

  • 使用place_type='TYPE_BUS_STATION' query_result = google_places.nearby_search( location=str(latLNG[z]).replace('(','').replace(')',''), radius=700, types=getattr(types, place_type)) 为所有类型提供输出,例如atmfoodschool 等,而不仅仅是BUS_STATION
猜你喜欢
  • 2019-10-26
  • 2016-11-08
  • 2015-05-16
  • 1970-01-01
  • 2011-10-31
  • 2014-11-18
  • 1970-01-01
  • 2014-12-31
  • 1970-01-01
相关资源
最近更新 更多