我在扩展Python的Rdio模块时遇到NameError

2024-05-18 10:09:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用Python来访问rdioapi,所以决定在Rdio模块中添加一些方法,以使工作更简单。我总是被阻碍。在

以下是公司提供的一些Rdio Python模块作为背景:

class Rdio:
  def __init__(self, consumer, token=None):
    self.__consumer = consumer
    self.token = token

  def __signed_post(self, url, params):
    auth = om(self.__consumer, url, params, self.token)
    req = urllib2.Request(url, urllib.urlencode(params), {'Authorization': auth})
    res = urllib2.urlopen(req)
    return res.read()

  def call(self, method, params=dict()):
    # make a copy of the dict
    params = dict(params)
    # put the method in the dict
    params['method'] = method
    # call to the server and parse the response
    return json.loads(self.__signed_post('http://api.rdio.com/1/', params))

好吧,一切都很好。这些功能工作正常。所以我决定创建一个方法,将带有key1的播放列表复制到key2的播放列表中。代码如下:

^{pr2}$

如果我从终端或在外部Python文件中执行此代码,则该代码可以正常工作,但由于某些原因,当我将其作为Rdio类的一部分时,然后将Rdio对象初始化为rdio并调用playlist方法,我总是会得到以下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "rdio_extended.py", line 83, in copy_playlist

NameError: global name 'rdio' is not defined

我好像绕不开这个。可能有一个简单的答案-我对编程很陌生-但我被难住了。在

更新:更新了代码格式,下面是创建Rdio对象的实际代码:

rdio = Rdio((RDIO_CONSUMER_KEY, RDIO_CONSUMER_SECRET), (RDIO_TOKEN, RDIO_TOKEN_SECRET))

下面是调用播放列表复制函数的行:

rdio.copy_playlist(key1, key2)

这将导致上述名称错误。在


Tags: the方法代码selftokenurlconsumerdef