0

I'm trying to access a class from another class but I'm running into this issue.

This is the class I'm trying to access.

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

And this is a scaled down version of the class where I'm having the error.

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")

The error in question is this:

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
Jensina Mart
  • 55
  • 1
  • 5
  • Possible duplicate of [AttributeError: 'module' object has no attribute](http://stackoverflow.com/questions/1250103/attributeerror-module-object-has-no-attribute) – midori Jan 28 '16 at 05:58

2 Answers2

1

In game_manager.py, line 241 (not shown in the question) your code is calling test(""), but test() does not accept any explicit arguments. It is an instance method. You should be calling it like this:

self.test()

i.e. as a method of a GameManager instance with no arguments.

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • I tried but that doesn't work. This problem also existed before that bit of code was added. I'm just using that as a shortcut to get to that error. – Jensina Mart Jan 28 '16 at 17:03
1

line 241, in GameManager test("")

You are passing an empty string to the test function as self. You should be passing a class instance. Actually you shouldn't be passing anything because python will do it for you if your calling convention and class definition is correct.

SpliFF
  • 38,186
  • 16
  • 91
  • 120
  • That was added as a shortcut. It doesn't affect the problem. – Jensina Mart Jan 28 '16 at 18:38
  • I disagree. As defined by your question it is entirely the problem. `""` is the `str` object you are passing as `self`. You think it is an instance of class GameManagerbut it isn't because you are calling it wrong. Since `self` *isn't* a GameManager instance the error you provided describes the problem you provided. If it's a "shortcut" then your shortcut is broken and you need to provide code and errors that actually reflect your problem. You are wasting peoples time until you do. Don't just show us the class, show us how you are creating and calling it. – SpliFF Jan 30 '16 at 00:37