I'm recently started to learn python and I'm playing with tweepy. now I'm kinda confused what this piece of code does especially the class part and what is passed to the class.
made some research about python classes and objects but still didn't get much out of it.
class PrintListener(tweepy.StreamListener):
def on_data(self, data):
tweet = json.loads(data)
print('@%s: %s' % (tweet['user']['screen_name'], tweet['text'].encode('ascii', 'ignore')))
def on_error(self, status):
print(status)
1 Answer 1
From Tweepy website:
In Tweepy, an instance of tweepy.Stream establishes a streaming session and routes messages to StreamListener instance. The on_data method of a stream listener receives all messages and calls functions according to the message type. The default StreamListener can classify most common twitter messages and routes them to appropriately named methods, but these methods are only stubs.
So basically, upon success, the on_data method will receive the messages as a JSON file and print the tweet as well as information about the author.
class PrintListener(tweepy.StreamListener):would like to know what is being passed to the class.classinheritanceoverridingjsonand more. which part you don't get?classdeclarations. (Hint:tweepy.StreamListeneris a superclass ofPrintListener)class