This question already has answers here:
TypeError: Missing 1 required positional argument: 'self'
                                
                                    (5个答案)
                                
                        
                                4年前关闭。
            
                    
我正在使用geopy,并对为什么会出现错误有疑问。

此代码示例来自github提供的代码示例。如前所述

from geopy.geocoders import Nominatim

geolocator = Nominatim()

location = geo.geocode("NY")

print((location.latitude, location.longitude))


下面的代码为什么会提供错误?背后的原因是什么?

from geopy.geocoders import Nominatim as geo

location = geo.geocode("NY")

print((location.latitude, location.longitude))


第二个代码提供的错误是:

Traceback (most recent call last):
  File "C:/Users/Leb/Desktop/Python/so2.py", line 5, in <module>
    location = geo.geocode("NY")
TypeError: geocode() missing 1 required positional argument: 'query'

最佳答案

您需要实例化类(创建对象)

from geopy.geocoders import Nominatim as geo

location = geo().geocode("NY")

print((location.latitude, location.longitude))

关于python - 调用类(class)[重复项目],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30968930/

10-12 20:13