相关疑难解决方法(0)

Python中的旧样式和新样式类有什么区别?

Python中的旧样式和新样式类有什么区别?我什么时候应该使用其中一种?

python oop types class new-style-class

953
推荐指数
8
解决办法
22万
查看次数

在Python 3中扩展一个类并使用__init__构造它

我想扩展datetime.date类,为它添加一个名为的属性status,表示日期是工作日,行政非工作日,法院关闭日,......

我已经阅读过如何在python中扩展一个类?,如何在python中扩展Python类init和链调用父构造函数,但我不太了解它,所以我是OOP的noob.

>>> import datetime
>>> class Fecha(datetime.date):
        def __init__(self, year, month, day, status):
            super(Fecha, self).__init__(self, year, month, day)
            self.status = status

>>> dia = Fecha(2014, 7, 14, 'laborable')
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    dia = Fecha(2014, 7, 14, 'laborable')
TypeError: function takes at most 3 arguments (4 given)
>>> 
Run Code Online (Sandbox Code Playgroud)

python oop inheritance class python-3.x

2
推荐指数
1
解决办法
973
查看次数

标签 统计

class ×2

oop ×2

python ×2

inheritance ×1

new-style-class ×1

python-3.x ×1

types ×1