【问题标题】:twython inheritance variabletwython 继承变量
【发布时间】:2016-06-01 12:19:19
【问题描述】:

我正在尝试在 twython 中扩展一个类 TwythonStreamer。跟随: [Inheritance and Overriding __init__ in python。重新定义工作正常的 on_success 方法。我的问题是添加一个计数变量并将其初始化为零。我收到错误“TypeError:init() 只需要 1 个参数(给定 5 个)”,因为我搞砸了 init()。

from twython import TwythonStreamer

C_KEY = "my_key"
C_SECRET = ""
A_TOKEN = ""
A_SECRET = ""

class MyStreamer(TwythonStreamer):
    def __init__(self):
        super(MyStreamer, self).__init__()
        self.count = 0
    def on_success(self, data):
        if 'text' in data:
            self.count += 1
            print("found it.", self.count)

stream = MyStreamer(C_KEY, C_SECRET, A_TOKEN, A_SECRET)

stream.statuses.filter(track="Primary")

【问题讨论】:

  • 您的__init__ 需要为您传递的所有内容(加上self)提供一个参数。您尚未在其签名中添加任何参数。

标签: python class twython


【解决方案1】:

我通过在创建流后添加解决了我的问题:

stream.count = 0

并删除我的 init

现在我可以在对象中使用 self.count。 这行得通,但充其量是不优雅的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-09
    • 2014-10-06
    • 1970-01-01
    • 2018-08-13
    • 2012-11-05
    • 2013-04-19
    • 2012-08-11
    • 2018-03-12
    相关资源
    最近更新 更多