Hi everyone,
I'd like to create a child class to the date class in the datetime module and, apart from adding some new methods to it, I'd like my child class to be initializable through different argument arrangements.

For example, beyond the (year, month, day) initialization arguments of the parent, I'd like the child class to be also initializable through a string, say, '1Jan2009', where the __init__ () method of the child is called with *args and depending on the arguments it resolves what (year, month, day) list it should use to initialize the parent, something like:

class DatePlus(date):
    def __init__(self, *args):
        if len(args) == 3:
            (y, m, d) = args
        elif len(args) == 1 and type(args[0]) == type(''):
            (y, m, d) = self.translate_string(self, arg[0])
        date.__init__(self, y, m, d)

However, I can't seem to be able to initialize the child class with a single argument; I get a TypeError that 3 arguments are required, 1 given.

Is there a way to accomplish what I'm trying to do or should the child class always be called with the same argument list structure as the parent?

Many thanks and have a happy new year.

Nowadays you should do : super(DatePlus, self).__init__(y,m,d) I think. Instead of explicitly calling __init__

Also change this: (y, m, d) = self.translate_string(self, arg[0]) to this: (y, m, d) = self.translate_string(arg[0]) Also, type('') can be replaced with str

Thanks, jcao219.

Still, even if I use super, I think the fact alone that the parent and child classes have mismatching argument lists in their initialization causes the TypeError I'm straggling with.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.