0

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)
asked Jun 7, 2019 at 0:04
5
  • more on this part: class PrintListener(tweepy.StreamListener): would like to know what is being passed to the class. Commented Jun 7, 2019 at 0:08
  • 1
    There are many things in this piece of code, there is a class inheritance overriding json and more. which part you don't get? Commented Jun 7, 2019 at 0:09
  • Or if its possible for you guys to explain what the entire code does line by line. i know its a lame question but could not find any better place to seek help from. Commented Jun 7, 2019 at 0:11
  • @ShahinShateri - I think you would be better off if you read a tutorial on Python. Read about class declarations. (Hint: tweepy.StreamListener is a superclass of PrintListener) Commented Jun 7, 2019 at 0:13
  • @StephenC thanks buddy. guess im starting to realize whats going on. and for sure will read more about python class Commented Jun 7, 2019 at 0:20

1 Answer 1

2

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.

answered Jun 7, 2019 at 0:09
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.