【问题标题】:Python AttributeError: 'super' object has no attribute 'testnet', however the attribute appears when __dict__ is called on super?Python AttributeError:'super'对象没有属性'testnet',但是当在super上调用__dict__时出现该属性?
【发布时间】:2021-04-27 13:34:05
【问题描述】:
from binance.client import Client
from binance.websockets import BinanceSocketManager

class Binance_Data(Client):
    def __init__(self, api_key, api_secret, requests_params=None, tld='us'):
        super().__init__(api_key, api_secret, requests_params=None, tld='us')

    def data_stream_test(self, data):
        print('------------------')
        print(f"Event Title: {data['e']}")
        print(f"Closing Price: {data['c']}")
        print(convert_unix_to_utc(data['E']))
        print('------------------')

    def data_stream(self):
        ds = BinanceSocketManager(super())
        conn_key = ds.start_symbol_ticker_socket('XLMUSDT', data_stream_test)
        ds.start()

我有这个类继承币安客户端从其 API 读取数据。然后我调用 BinanceSocketManager,它接收 binance 客户端的一个实例。但是,当我运行程序时,出现以下错误:

AttributeError: 'super' object has no attribute 'testnet'

但是当我在测试方法中打印 super().__dict__ 以获取属性时,它会将 testnet 显示为 super 的属性:

{...,'testnet': False, 'timestamp_offset': -8}

我调用 super().get_ticker() 等父方法没有问题,但是这里出现错误。

【问题讨论】:

  • 你为什么在这里使用super(),而不仅仅是self?你的类的一个实例也是Client的一个实例。
  • @jasonharper 是的,你是对的,谢谢!
  • @jasonharper 做出回答,以便我将其标记为已回答并投票给你

标签: python python-3.x attributeerror


【解决方案1】:

super() 不返回类本身,而是调用超类方法的代理,讨论了here

试试self,而不是super()

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    相关资源
    最近更新 更多