TypeError:必须使用'class name'实例作为第一个参数调用未绑定方法'method name'(改为使用str实例)

时间:2013-12-12 17:56:39

标签: 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 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 我只是想按照那里找到的例子。

任何帮助都会很棒!

3 个答案:

答案 0 :(得分:1)

根据Github上的@qazwsxpawel的建议,@ staticmethod修饰符已添加到TMDB类方法_request和_set_attrs_to_values中。如果你升级你的tmdbsimple版本,这些例子现在应该在Python 2.7中运行。 https://pypi.python.org/pypi/tmdbsimple

答案 1 :(得分:0)

它可能与您对方法_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

请参阅this post,解释了使用from foo import *语法时未导入单个前导下划线方法的原因

答案 2 :(得分:0)

这看起来像一个简单的拼写错误。变化:

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

对此:

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