0

I did create a new datetime.date like object

class WorkDate(datetime.date):
    pass

The functionality of this WorkDate is(not important for this question) that when you add a timedelta object it will only move to a weekday date. ie when you add a timedelta(1) on a Friday dated WorkDate it will return the next Monday dated WorkDate.


How can I __init__ WorkDate by any one of these two method to create same result
x = WorkDate(2017, 8, 3)
y = WorkDate(datetime.date(2017, 8, 3))


I did try this but not working for initialization with date object
class WorkDate(datetime.date):
    def __init__(self, *args):
        if len(args) == 3:
            super(WorkDate, self).__init__(args[0], args[1], args[2])
        elif len(args) == 1:
            self.year = args[0].year
            self.month = args[0].month
            self.day = args[0].day
mahes
  • 1,074
  • 3
  • 12
  • 20
  • If the dupe doesn't help you, do let us know how and why. – cs95 Aug 20 '17 at 10:33
  • @cᴏʟᴅsᴘᴇᴇᴅ can't understand why i am not able to initialize with date object in my code. It says *an integer is required* when i try to construct with a date object – mahes Aug 20 '17 at 13:28

1 Answers1

-1

Since you want to support any number of arguments, accept *args .

Then, in your __init__, pass these to the __init__ of the base class.

def __init__(*args):
    super(WorkDate, self).__init__(*args)

Here's a version for Python 3, with added support for named (keyword) arguments `**kwargs':

def __init__(*args, **kwargs):
    super().__init__(*args, **kwargs)
florisla
  • 12,668
  • 6
  • 40
  • 47