如何忽略AttributeError:' NoneType'

时间:2015-09-24 18:22:13

标签: python attributeerror nonetype geopy

location = geolocator.geocode(" ".join(address.values()))
if location.longitude is not None:
    node['pos'] = [location.longitude, location.latitude]

不要理解我仍然会收到此错误:

  File "/home/easypc/Documents/Udacity_nano_degree/Data_Wrangling/audit_vilnius.py", line 167, in shape_element
    if location.longitude is not None:
AttributeError: 'NoneType' object has no attribute 'longitude'

1 个答案:

答案 0 :(得分:7)

location变量本身就是None,也可以对此进行测试:

if location is not None and location.longitude is not None:

或者更简单地说:

if location: