进行面向对象的编程时,对象没有属性“方法”

时间:2019-08-28 16:21:43

标签: python-3.x oop object methods

我创建了一个名为“ User”的类,它具有几个不同的方法。但是,当我添加新方法时,我无法获取代码来识别它。

class User(object):

    def __init__(self, username, password, sess=None):
        self.username = username
        if sess == None:
            sess = requests.Session()

        req = sess.get('https://archiveofourown.org')
        soup = BeautifulSoup(req.text, features='html.parser')

        authenticity_token = soup.find('input', {'name': 'authenticity_token'})['value']

        req = sess.post('https://archiveofourown.org/user_sessions', params={
            'authenticity_token': authenticity_token,
            'user_session[login]': username,
            'user_session[password]': password,
        })

        # Unfortunately AO3 doesn't use HTTP status codes to communicate
        # results -- it's a 200 even if the login fails.
        if 'Please try again' in req.text:
            raise RuntimeError(
                'Error logging in to AO3; is your password correct?')

        self.sess = sess

    def __repr__(self):
        return '%s(username=%r)' % (type(self).__name__, self.username)

    def work_ids(self):
        return 0

init和repr正常​​工作。但是,当我尝试使用work_ids方法(稍后添加并后来简化为“返回0”以找出问题所在)时,如下所示:

from ao3 import AO3

api = AO3()
api.login('username', 'password')
myworks = api.user.work_ids()

我收到以下错误:

runfile('C:/Users/H/Desktop/ao3/examples/ao3stats.py', wdir='C:/Users/H/Desktop/ao3/examples')
Traceback (most recent call last):

  File "<ipython-input-12-de3e7c373274>", line 1, in <module>
    runfile('C:/Users/H/Desktop/ao3/examples/ao3stats.py', wdir='C:/Users/H/Desktop/ao3/examples')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/H/Desktop/ao3/examples/ao3stats.py", line 9, in <module>
    myworks = api.user.work_ids()

AttributeError: 'User' object has no attribute 'work_ids'

起初我以为我有缩进或空格问题,所以我在视觉上和通过我的IDE(Spyder)中的自动功能仔细检查了所有制表符和空格是否一致。我还重置了名称空间,并关闭并重新打开了IDE,以防我持有一个旧变量,但这没用。我还检查了我的命名是否始终一致。如何获得“用户”对象以识别我编写的“ work_ids”方法?

0 个答案:

没有答案