繁体   English   中英

TypeError:未绑定方法“方法名称”必须以“类名称”实例作为第一个参数来调用(取而代之的是str实例)

[英]TypeError: unbound method 'method name' must be called with 'class name' instance as first argument (got str instance instead)

嗨,我正尝试将simpletmdb python包装器用于“电影数据库” API,但我无法通过此问题。

当我尝试创建对象并调用获取电影信息的方法时,我一直收到此错误。

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

可以在这里找到包装器https://github.com/celiao/tmdbsimple我只是想按照那里找到的示例进行操作。

任何帮助将是巨大的!

正如Github上@qazwsxpawel所建议的那样,@ staticmethod装饰器已添加到TMDB类方法_request和_set_attrs_to_values。 如果升级您的tmdbsimple版本,这些示例现在应该可以在Python 2.7中使用。 https://pypi.python.org/pypi/tmdbsimple

可能与您缩进_request方法_request 试试这个代码:

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

请参阅这篇文章 ,其中解释了为什么from foo import *语法中使用时,不会导入单个前导下划线方法

这看起来像是一个简单的错字。 更改:

    def _request(method, path, params={}, json_body={}):

对此:

    def _request(self, method, path, params={}, json_body={}):

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM