0
import praw import time class getPms(): r = praw.Reddit(user_agent="Test Bot By /u/TheC4T") r.login(username='*************', password='***************') cache = [] inboxMessage = [] file = 'cache.txt' def __init__(self): cache = self.cacheRead(self, self.file) self.bot_run(self) self.cacheSave(self, self.file) time.sleep(5) return self.inboxMessage def getPms(self): def bot_run(): inbox = self.r.get_inbox(limit=25) print(self.cache) # print(r.get_friends())#this works for message in inbox: if message.id not in self.cache: # print(message.id) print(message.body) # print(message.subject) self.cache.append(message.id) self.inboxMessage.append(message.body) # else: # print("no messages") def cacheSave(self, file): with open(file, 'w') as f: for s in self.cache: f.write(s + '\n') def cacheRead(self, file): with open(file, 'r') as f: cache1 = [line.rstrip('\n') for line in f] return cache1 # while True: #threading is needed in order to run this as a loop. Probably gonna do this in the main method though # def getInbox(self): # return self.inboxMessage 

The exception is:

 cache = self.cacheRead(self, self.file) AttributeError: 'getPms' object has no attribute 'cacheRead' 

I am new to working with classes in python and need help with what I am doing wrong with this if you need any more information I can add some. It worked when it was all functions but now that I attempted to switch it to a class it has stopped working.

1
  • You didn't prefix cache with self. within init which must be done to make it accessible throughout the class. And you have a namespace conflict (init is the constructor, don't name methods with class name in Python) Commented Jun 16, 2016 at 23:51

2 Answers 2

3

Your cacheRead function (as well as bot_run and cacheSave) is indented too far, so it's defined in the body of your other function getPms. Thus it is only accessible inside of getPms. But you're trying to call it from __init__.

I'm not sure what you're trying to achieve here because getPms doesn't have anything else in it but three function definitions. As far as I can tell you should just take out the def getPms line and unindent the three functions it contains so they line up with the __init__ method.

Sign up to request clarification or add additional context in comments.

Comments

0

Here are few points:

  1. Unless you're explicitly inheriting from some specific class, you can omit parenthesis:

class A(object):, class A():, class A: are equivalent.

  1. Your class name and class method have the same name. I'm not sure does Python confuse about this or not, but you probably do. You can name your class PMS and your method get, for example, so you'll obtain PMS.get(...)

  2. In the present version of indentation cacheRead and cacheSave functions are simply inaccessible from init; why not move them to generic class namespace?

  3. When calling member functions, you don't need to specify self as the first argument since you're already calling the function from this object. So instead of cache = self.cacheRead(self, self.file) you have to do it like this: cache = self.cacheRead(self.file)

1 Comment

Under Python versions < 3, class A(object): and class A: are not equivalent. The version that derives from object is a new-style class, see e.g. stackoverflow.com/questions/54867/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.