0

I'm trying to filter tweets by hashtag and output the text of the Tweet.

The class Tweet contains the overarching tweet info, of which I only need "text". I know that str method needs to be used to call each instance of the class, but down below when I "print", the above class is inaccessible. Please help :D

Edit: what's the implication of replacing __str__ with __repr__ in class Tweet? Thanks!

class Tweet():

    tweet = Tweet()
    def __init__(self, tweet_dict = {}):        
        if "statuses" in tweet_dict:
            self.status = tweet_dict["statuses"]
        else:
            self.status = ""

        if "text" in self.status:
            self.text = item["text"]
        else:
            self.text = ""

    def __str__(self):
        tweet_info = self.text
        return tweet_info

#-------------------------------------------------------------------

tweet_inst = []
for dic in statuses:
    item_instances = Tweet(dic)
    tweet_inst.append(item_instances)
print(tweet_inst)
Nick P
  • 1
  • 1
  • Could you please fix the code block formatting? The `tweet = Tweet()` line looks fishy, but I can't tell if it is actually part of the class body. If it is, this will be one of your problems I guess. – shmee Apr 10 '18 at 06:06

2 Answers2

0

__str__ needs to return a string representation of the object.

__str__ goal is to be readable, __repr__ goal is to be unambiguous.

When you print a list (which is tweet_inst), it iterates over the objects (of Tweet) contained in the list and call their __repr__.

You want to add def __repr__(self): return str(self) so they do the same thing, then it will return self.text instead of the object default representation (which is it's location in memory)

class Tweet(object):

    def __init__(self, x):
        self.x = x

    def __str__(self):
        return self.x

    def __repr__(self):
        return '__repr__' + str(self.x)

>>> t1 = Tweet('t1')
>>> t2 = Tweet('t2')
>>> list_ = [t1, t2]
>>> print(list_)
[__repr__t1, __repr__t2]

Rather than the default
[<__main__.Tweet object at 0x02AFA810>, <__main__.Tweet object at 0x029E2BF0>]

Here is a great answer by moshez about the difference between the two.

Chen A.
  • 10,140
  • 3
  • 42
  • 61
  • Actually, I think the OP does return tweet_info in `__str__`, just the formatting of the code block is broken, which is why I asked to fix it. – shmee Apr 10 '18 at 07:06
  • @shmee You're right, it was out of the code block, written in bold so I didn't notice. – Chen A. Apr 10 '18 at 07:24
0

First of all, the second line, tweet = Tweet() in the class definition is wrong. You cannot create a class variable containing an instance of said class.

Second, you assign self.text = item["text"] in line 11. What is item? Should that rather be tweet_dict?

Third, you could directly return self.text in __str__, no need to assign it to a varibale tweet_info first.

Last, but not least, you should not use mutable datatypes as default arguments, like you do in line 4. Here's why.

shmee
  • 4,721
  • 2
  • 18
  • 27