python - 在python中,frozenset的子类的__init__方法抛出参数编号的TypeError
问题描述
该类的__init__
方法有 3 个参数,但是当我用 3 个参数实例化它时,它会抛出一个错误,它需要 1 个参数。我无法理解。
class ArrObj(frozenset):
def __init__(self, elem_list, elem_count, self_count):
super(ArrObj, self).__init__(elem_list) # Enums, ArrObj, race_id
self.elem_count = elem_count
self.self_count = self_count
assert self_count > 0
if __name__ == '__main__':
a = ArrObj(['a', 'b', 'c'], {'a':1, 'b':2, 'c':3}, 8)
Traceback (most recent call last):
File "G:/pycharm-projects/new_keyinfo/verify_treekeys.py", line 34, in <module>
a = ArrObj(['a', 'b', 'c'], {'a':1, 'b':2, 'c':3}, 8)
TypeError: ArrObj expected at most 1 arguments, got 3
解决方案
frozenset.__init__
不需要额外的参数,因为你不能frozenset
在它被创建后修改它。(事实上,frozenset
根本没有定义__init__
;它只是使用__init__
it 继承自object
。)您传递给的可迭代frozenset
对象被 by 消耗frozenset.__new__
。
class ArrObj(frozenset):
def __new__(cls, elem_list, elem_count, self_count):
# May as well assert this before you do any more work
assert self_count > 0
obj = super().__new__(cls, elem_list)
obj.elem_count = elem_count
obj.self_count = self_count
return obj
推荐阅读
- php - 我可以使用 iframe 从非面向互联网的网络服务器嵌入另一个网页吗
- javascript - 将无状态功能组件反应到类 Component
- rabbitmq - 使用 HAProxy 作为 RabbitMQ 的负载均衡器是否不是最佳选择?
- firefox - 在使用 SeleniumGrid 创建会话时,Firefox 实例在 Windows 8.1 和 7 上无法与 Fiddler 一起使用
- google-apps-script - 检索数据避免谷歌配额
- java - 正在寻找 SpringBoot Hystrix Dashboard 解释(SpringBoot Starter)安全启用的 Hystrix Stream(作为它自己的项目)的解决方案?
- splunk - Splunk 生成一个只有一行的表
- angular - Angular 7 表单未在 ios 键盘上显示“开始”按钮
- c# - 使用 RestSharp 在 MySql 数据库中发布
- vue.js - 为什么同一个 vue 组件上的多个引导下拉菜单共享相同的选项?