Python 在新样式类中继承旧样式类型

标签 python inheritance

我正在使用 csv.DictWriter 类,我想继承它:

class MyObj(csv.Dictwriter):
    ...

但是这个类型是一个老式的对象。 MyObj 可以是新式类,但仍然继承自 csv.DictWriter 吗?

最佳答案

是的,你也只需要从 object 继承:

class MyObj(object, csv.DictWriter):
    def __init__(self, f, *args, **kw):
        csv.DictWriter.__init__(self, f, *args, **kw)

关于Python 在新样式类中继承旧样式类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34842022/

相关文章:

java - Apache Thrift Python-Java 'Connection Refused'

javascript - 从 Webix 中的 View 继承 CSS

hibernate 故障 : INSERT instead of UPDATE when using Inheritance:SINGLE_TABLE and SecondaryTables

python - Python 中关于多重继承的怪癖!至少我是这么认为的

javascript - 原型(prototype)继承javascript

python - 将 Pandas 数据框转换为以第一列为键的字典

python - kaa编辑器的入口点是什么? (在 Linux 上卡住 kaa)

python - 如何让 Bokeh 悬停工具捕捉数据?

python - 操作错误(2003、 "Can' t 连接到 '127.3.138.130' (111) 上的 MySQL 服务器")

android - 在 kotlin 中,如何在目标需要 List 的地方传回 MutableList