0

I've been trying to add a functionality to a class, without changing its behavior. But it gives an AttributeError. How do I code this? (PS: You don't need to know praw for this)


In the following: the Comment object is returned from Reddit object.

from praw import Reddit
from praw.models import Comment

class Comment(Comment):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        print("this is my Comment")
        
    def is_awesome(self):
        return True
        
class Reddit(Reddit):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        print("this is my Reddit")

# this is my Reddit object
reddit = Reddit("some args")

# this is a Comment object from praw 
k = reddit.comment(id="hzfbs3e")

# this gives an `AttributeError`
k.is_awesome()

# for example there is also this
k.author # which returns a `Redditor` object
korfor
  • 1
  • 1
  • You didn't modify the `Comment` class; you created another class also called `Comment`. `k` is an instance of the original class `Comment`, not your class. – khelwood Mar 05 '22 at 12:26
  • The [linked dupe](https://stackoverflow.com/q/972/3890632) shows how to add a method to a class (among other options). – khelwood Mar 05 '22 at 12:30
  • It seems you are calling a function `is_awesome` that is defined in the class `Comment`. You don't extend the class as you should, the class `Reddit` should take `Comment` as an argument. – Jonathan Peil Mar 05 '22 at 12:32

0 Answers0