AttributeError:&#39; str&#39;对象没有属性<class name =“”>

时间:2016-01-28 05:30:32

标签: python import

我试图从另一个班级访问一个班级,但我遇到了这个问题。

这是我尝试访问的课程。

import tweepy

ckey= '**********************'
csecret= '**********************'
atoken= '**********************'
asecret= '**********************'

class TwtPrinter:
    def printTweet(self, user, text):
        auth = tweepy.OAuthHandler(ckey, csecret)
        auth.set_access_token(atoken, asecret)
        api = tweepy.API(auth)
        for status in tweepy.Cursor(api.user_timeline).items():
            try:
                api.destroy_status(status.id)
            except:
                pass

这是该课程的缩小版本,我有错误。

import sqlite3
import random
from app.models.monDAO import monDAO
from app.models.charDAO import CharDAO
from app.models.dunDAO import DunDAO
from app.controllers.twt_print import TwtPrinter







class GameManager:

    def testDB(self):
        print("hello world")
        conn = sqlite3.connect('DunSuciRun.sqlite')
        c = conn.cursor()

        this = """SELECT * FROM CHARACTERS"""
        c.execute(this)
        getStuff = c.fetchall()

        charTuple = getStuff[0]
        cha = CharDAO(charTuple[0], charTuple[1],charTuple[2],charTuple[3],charTuple[4])
        print(cha.name.split(' ')[0])


    def test(self):
        self.twt_print = TwtPrinter
        testing = "testing"
        print testing
        # self.twt_print = TwtPrinter
        self.twt_print.printTweet("1""2")

有问题的错误是:

C:\Python27\python.exe C:/Users/Jensi/PycharmProjects/DSR.02/app/controllers/game_manager.py
Traceback (most recent call last):
  File "C:/Users/Jensi/PycharmProjects/DSR.02/app/controllers/game_manager.py", line 14, in <module>
    class GameManager:
  File "C:/Users/Jensi/PycharmProjects/DSR.02/app/controllers/game_manager.py", line 241, in GameManager
    test("")
  File "C:/Users/Jensi/PycharmProjects/DSR.02/app/controllers/game_manager.py", line 32, in test
    self.twt_print = TwtPrinter
AttributeError: 'str' object has no attribute 'twt_print'

Process finished with exit code 1

2 个答案:

答案 0 :(得分:1)

game_manager.py,第241行(问题中未显示),您的代码正在调用test(""),但test()不接受任何显式参数。这是一个实例方法。你应该这样称呼它:

self.test()

即。作为没有参数的GameManager实例的方法。

答案 1 :(得分:1)

line 241, in GameManager test("")

您将空字符串作为test传递给self函数。你应该传递一个类实例。实际上你不应该传递任何东西,因为如果你的调用约定和类定义是正确的,python会为你做这件事。