简体   繁体   中英

TypeError: __init__() takes at least 4 non-keyword arguments (3 given)

Advice please :)

When I use this script:

class CustomStreamListener(tweepy.StreamListener):

    def on_status(self, status):

        # We'll simply print some values in a tab-delimited format
        # suitable for capturing to a flat file but you could opt 
        # store them elsewhere, retweet select statuses, etc.



        try:
            print "%s\t%s\t%s\t%s" % (status.text, 
                                      status.author.screen_name, 
                                      status.created_at, 
                                      status.source,)
        except Exception, e:
            print >> sys.stderr, 'Encountered Exception:', e
            pass

    def on_error(self, status_code):
        print >> sys.stderr, 'Encountered error with status code:', status_code
        return True # Don't kill the stream

    def on_timeout(self):
        print >> sys.stderr, 'Timeout...'
        return True # Don't kill the stream

# Create a streaming API and set a timeout value of 60 seconds.

streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)

# Optionally filter the statuses you want to track by providing a list
# of users to "follow".

print >> sys.stderr, 'Filtering the public timeline for "%s"' % (' '.join(sys.argv[1:]),)

streaming_api.filter(follow=None, track=Q)

There was an error like this:

Traceback (most recent call last):
  File "C:/Python26/test.py", line 65, in <module>
    streaming_api = tweepy.streaming.Stream(auth, CustomStreamListener(), timeout=60)
TypeError: __init__() takes at least 4 non-keyword arguments (3 given)

What should I do then?

Your example appears to be from here . And you are using Tweepy , a Python library for accessing the Twitter API.

From Github, here is the definition of a Stream() object (assuming you have the latest version of Tweepy, please double check!),

def __init__(self, auth, listener, **options):
        self.auth = auth
        self.listener = listener
        self.running = False
        self.timeout = options.get("timeout", 300.0)
        self.retry_count = options.get("retry_count")
        self.retry_time = options.get("retry_time", 10.0)
        self.snooze_time = options.get("snooze_time",  5.0)
        self.buffer_size = options.get("buffer_size",  1500)
        if options.get("secure"):
            self.scheme = "https"
        else:
            self.scheme = "http"

        self.api = API()
        self.headers = options.get("headers") or {}
        self.parameters = None
        self.body = None

Because you seemed to have passed in the appropriate number of arguments, it looks like CustomStreamListener() isn't being initialized, and therefore isn't being passed to the Stream() class as an argument. See if you can initialize a CustomStreamListener() prior to being passed as an argument to Stream() .

__init__ is the constructor of a class, in this case Stream . The error means you have provided a wrong number of arguments to the constructor call.

The main reason of this issue is using the older version of tweepy. I was using tweepy 1.7.1 and having the same error before I updated tweepy 1.8. Right after that, the issue was solved. I think the 4 voted answer should be accepted answer to clarify the solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM