Skip to main content
 首页 » 编程设计

python之类型错误:必须使用 'method name' 实例作为第一个参数调用未绑定(bind)方法 'class name'(改为获取 str 实例)

2025年05月04日9Renyi-Fan

您好,我正在尝试将 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 包装器的必要部分是,电影类是 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 

可以在此处找到 Wrapper https://github.com/celiao/tmdbsimple 我只是想按照那里找到的例子。

任何帮助都会很棒!

请您参考如下方法:

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