TypeError: unbound method 'method name' must be called with 'class name' instance as first argument (got str instance instead)
嗨,我正在尝试将simpletmdb python包装用于"电影数据库"API,但我无法通过此问题。
当我试图创建对象并调用获取电影信息的方法时,我总是得到这个错误。
1 2 3 | 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) |
我的代码是:
1 2 3 4 5 6 | from tmdbsimple import TMDB tmdb = TMDB('API_KEY') movie = tmdb.Movies(603) response = movie.info() print movie.title |
simpleTMDB包装器的必要部分是,movies类是TMDB的一个子类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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 |
包装可在以下网址找到:https://github.com/celiao/tmdbsimple我只是想遵循在那里找到的例子。
任何帮助都会很好!
正如github上的@qazwsxpawel所建议的那样,@staticmethod修饰符已经添加到tmdb类方法请求中,并将属性设置为值。如果升级tmdbsimple的版本,这些示例现在应该可以在python 2.7中使用了。https://pypi.python.org/pypi/tmdbsimple
这看起来像一个简单的打字错误。变化:
1 | def _request(method, path, params={}, json_body={}): |
对此:
1 | def _request(self, method, path, params={}, json_body={}): |
这可能与您对方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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 |
请看这篇文章,它解释了为什么在使用