'list'对象没有属性

时间:2017-08-26 05:05:27

标签: python list syntax attributes

嗯,我不知道为什么它认为这个函数变量是list。问题发生在编译时此代码的第41行(在addPoint()函数中)。我已经尝试在其位置使用user全局变量,但它仍然无效。我觉得有一些简单的东西,我错过但不能确定。

import praw
import time
import re
import pickle
from classes import User



USERAGENT = 'web:CredibilityBot:v0.1 (by /u/ThePeskyWabbit)'
FOOTER = "^^I ^^am ^^a ^^bot! ^^I ^^am ^^currently ^^in ^^test ^^phase. ^^Read ^^about ^^me ^^[here](https://pastebin.com/jb4kBTcS)."
PATH = "C:\\Users\\JoshLaptop\\PycharmProjects\\practice\\commented.txt"

user = User.User("placeholder")
commentFile = open(PATH, 'rb')
commentList = commentFile.read().splitlines()
commentFile.close()

pkl = open("userpkl.pkl", 'rb')
pklList = []

try:
    while(True):
        pklList.append(pickle.load(pkl))
except EOFError:
    pass
pkl.close()

nameList = []
try:
    for data in pklList:
        user = data
        nameList.append(str(user.getName()))
except:
    pass

print(pklList)
print(nameList)


def addPoint(userInstance, comment):
    userInstance.addScore()
    userInstance.addComment(comment)
    message = "Bullshit noted! " + userInstance.getName() + " now has a Bullshit rating of \n\n" + userInstance.getScore() + FOOTER
    return message

编写该方法的类如下:

import pickle
import praw

class User:
    def __init__(self, username):
        self.name = username
        self.bsRating = 0
        self.bsComments = []

    def getName(self):
        return self.name

    def getScore(self):
        return str(self.bsRating)

    def resetScore(self):
        self.bsRating = 0

    def getComments(self):
        return self.bsComments

    def addComment(self, commentLink):
        self.bsComments.append(commentLink)

    def addScore(self):
        self.bsRating = self.bsRating + 1

我的错误输出是:

  File "C:/Users/JoshLaptop/PycharmProjects/practice/TestBot.py", line 63, in <module>
    actions = {"!bullshit": addPoint(user, commentLink), "!bullshitrating": getRating(user), "!bullshitdetail":getCommentList(user)}
  File "C:/Users/JoshLaptop/PycharmProjects/practice/TestBot.py", line 41, in addPoint
    userInstance.addScore()
AttributeError: 'list' object has no attribute 'addScore'

1 个答案:

答案 0 :(得分:0)

发现了这个问题。问题出在这里试试:

for data in pklList:
    user = data
    nameList.append(str(user.getName()))
except:
    pass

已删除user = data并将user.getName()重写为data.getName(),问题已得到解决。非常奇怪