Why can't I subclass datetime.date?

后端 未结 6 1672
半阙折子戏
半阙折子戏 2020-11-29 08:30

Why doesn\'t the following work (Python 2.5.2)?

>>> import datetime
>>> class D(datetime.date):
        def __init__(self, year):
                  


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-29 08:54

    You should probably use a factory function instead of creating a subclass:

    def first_day_of_the_year(year):
      return datetime.date(year, 1, 1)
    

提交回复
热议问题