2011-08-01 75 views
1

建议,请:)类型错误:__init __()至少需要4个非关键字参数(3给出)

当我使用这个脚本:

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) 

有这样的错误:

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) 

那我该怎么办?

+0

您使用的是什么版本的Tweepy?它看起来像'tweepy.streaming.Stream'从[此提交]中取4个参数变为3个参数(https://github.com/tweepy/tweepy/commit/13814fdf1f2dead020abc70257a94ddfadf0deb#tweepy/streaming.py)。 – icktoofay

回答

1

__init__是一个类的构造函数,在这种情况下是Stream。该错误意味着您向构造函数调用提供了错误数量的参数。

4

您的示例似乎来自here。您正在使用Tweepy,一个用于访问Twitter API的Python库。

从GitHub,hereStream()对象的定义(假设你有最新版本的Tweepy的,请仔细检查!),

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 

因为你似乎在参数的适当数量的已经过去了,它看起来像CustomStreamListener()未被初始化,因此没有被传递给Stream()类作为参数。查看是否可以在作为参数传递给Stream()之前初始化CustomStreamListener()

0

这个问题的主要原因是使用老版本的tweepy。我在使用tweepy 1.7.1并且在更新tweepy 1.8之前发生了相同的错误。之后,问题解决了。我认为应该接受4票赞成的答案来澄清解决方案。

相关问题