Python 类型错误:未绑定方法';方法名称';必须使用';类名';实例作为第一个参数(改为获取str实例)
您好,我正在尝试为“电影数据库”API使用simpletmdb python包装,但我无法通过此问题 当我尝试创建对象并调用获取电影信息的方法时,我一直会遇到这个错误Python 类型错误:未绑定方法';方法名称';必须使用';类名';实例作为第一个参数(改为获取str实例),python,api,python-2.7,typeerror,Python,Api,Python 2.7,Typeerror,您好,我正在尝试为“电影数据库”API使用simpletmdb python包装,但我无法通过此问题 当我尝试创建对象并调用获取电影信息的方法时,我一直会遇到这个错误 in info response = TMDB._request('GET', path, params) TypeError: unbound method _request() must be called with TMDB instance as first argument (got str instan
in info
response = TMDB._request('GET', path, params)
TypeError: unbound method _request() must be called with TMDB instance as first argument (got str instance instead)
我调用它的代码是:
from tmdbsimple import TMDB
tmdb = TMDB('API_KEY')
movie = tmdb.Movies(603)
response = movie.info()
print movie.title
simpletmdb包装器的必要部分是,Movies类是TMDB的一个子类:
class TMDB:
def __init__(self, api_key, version=3):
TMDB.api_key = str(api_key)
TMDB.url = 'https://api.themoviedb.org' + '/' + str(version)
def _request(method, path, params={}, json_body={}):
url = TMDB.url + '/' + path + '?api_key=' + TMDB.api_key
if method == 'GET':
headers = {'Accept': 'application/json'}
content = requests.get(url, params=params, headers=headers).content
elif method == 'POST':
for key in params.keys():
url += '&' + key + '=' + params[key]
headers = {'Content-Type': 'application/json', \
'Accept': 'application/json'}
content = requests.post(url, data=json.dumps(json_body), \
headers=headers).content
elif method == 'DELETE':
for key in params.keys():
url += '&' + key + '=' + params[key]
headers = {'Content-Type': 'application/json', \
'Accept': 'application/json'}
content = requests.delete(url, data=json.dumps(json_body), \
headers=headers).content
else:
raise Exception('method: ' + method + ' not supported.')
response = json.loads(content.decode('utf-8'))
return response
#
# Set attributes to dictionary values.
# - e.g.
# >>> tmdb = TMDB()
# >>> movie = tmdb.Movie(103332)
# >>> response = movie.info()
# >>> movie.title # instead of response['title']
class Movies:
""" """
def __init__(self, id=0):
self.id = id
# optional parameters: language
def info(self, params={}):
path = 'movie' + '/' + str(self.id)
response = TMDB._request('GET', path, params)
TMDB._set_attrs_to_values(self, response)
return response
包装可以在这里找到
我只是想效仿那里的例子
任何帮助都会很好 这可能与方法的缩进有关
\u请求
。请尝试以下代码:
class TMDB:
def __init__(self, api_key, version=3):
TMDB.api_key = str(api_key)
TMDB.url = 'https://api.themoviedb.org' + '/' + str(version)
def _request(method, path, params={}, json_body={}):
url = TMDB.url + '/' + path + '?api_key=' + TMDB.api_key
if method == 'GET':
headers = {'Accept': 'application/json'}
content = requests.get(url, params=params, headers=headers).content
elif method == 'POST':
for key in params.keys():
url += '&' + key + '=' + params[key]
headers = {'Content-Type': 'application/json', \
'Accept': 'application/json'}
content = requests.post(url, data=json.dumps(json_body), \
headers=headers).content
elif method == 'DELETE':
for key in params.keys():
url += '&' + key + '=' + params[key]
headers = {'Content-Type': 'application/json', \
'Accept': 'application/json'}
content = requests.delete(url, data=json.dumps(json_body), \
headers=headers).content
else:
raise Exception('method: ' + method + ' not supported.')
response = json.loads(content.decode('utf-8'))
return response
#
# Set attributes to dictionary values.
# - e.g.
# >>> tmdb = TMDB()
# >>> movie = tmdb.Movie(103332)
# >>> response = movie.info()
# >>> movie.title # instead of response['title']
class Movies:
""" """
def __init__(self, id=0):
self.id = id
# optional parameters: language
def info(self, params={}):
path = 'movie' + '/' + str(self.id)
response = TMDB._request('GET', path, params)
TMDB._set_attrs_to_values(self, response)
return response
请参阅,这解释了为什么在使用来自foo import*语法的
时不导入单前导下划线方法。这看起来像一个简单的输入错误。更改:
def _request(method, path, params={}, json_body={}):
为此:
def _request(self, method, path, params={}, json_body={}):
正如Github上的@qazwsxpawel所建议的那样,@staticmethod decorators已添加到TMDB类方法请求和设置属性值中。如果您升级了tmdbsimple的版本,那么这些示例现在应该可以在Python2.7中使用了 我想可能是这样,但没什么区别。我用make_request更改了所有出现的_request,但仍然得到相同的错误。