inheriting from datetime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Conner

    inheriting from datetime

    So this is simple, why can't I run the following code? I've tried many
    variances of this, but simply cannot inherit from datetime or
    datetime.dateti me. I get this on line 3.
    TypeError: function takes at most 2 arguments (3 given)

    *************** *************** **
    import datetime
    _datetime = datetime.dateti me

    class MyDateTime(_dat etime):
    """
    Identical to builtin datetime.dateti me, except it accepts
    invalid dates and times as input.
    """
    _valid = True

    def __init__(self, year, month, day, *args, **kw):
    try:
    _datetime.__ini t__(self, year, month, day, *args, **kw)
    except _datetime.Value Error:
    _valid = False
    self.year = year
    self.month = month
    self.day = day
    self.args = args
    self.kw = kw
    *************** *************** **

  • Grant Edwards

    #2
    Re: inheriting from datetime

    On 2005-08-01, Rob Conner <rtconner@gmail .com> wrote:
    [color=blue]
    > So this is simple, why can't I run the following code? I've tried many
    > variances of this, but simply cannot inherit from datetime or
    > datetime.dateti me. I get this on line 3.
    > TypeError: function takes at most 2 arguments (3 given)[/color]

    I think I posted this question a while back and the problem was
    that a datetime instance is not mutable, so the normal method
    of deriving a class didn't work....

    A quick google...



    You need to override __new__ rather than __init__

    --
    Grant Edwards grante Yow! I'm ANN LANDERS!! I
    at can SHOPLIFT!!
    visi.com

    Comment

    • Lee Harr

      #3
      Re: inheriting from datetime

      On 2005-08-01, Rob Conner <rtconner@gmail .com> wrote:[color=blue]
      > So this is simple, why can't I run the following code? I've tried many
      > variances of this, but simply cannot inherit from datetime or
      > datetime.dateti me. I get this on line 3.
      > TypeError: function takes at most 2 arguments (3 given)
      >
      > *************** *************** **
      > import datetime
      > _datetime = datetime.dateti me
      >
      > class MyDateTime(_dat etime):
      > """
      > Identical to builtin datetime.dateti me, except it accepts
      > invalid dates and times as input.
      > """
      > _valid = True
      >
      > def __init__(self, year, month, day, *args, **kw):
      > try:
      > _datetime.__ini t__(self, year, month, day, *args, **kw)
      > except _datetime.Value Error:
      > _valid = False
      > self.year = year
      > self.month = month
      > self.day = day
      > self.args = args
      > self.kw = kw
      > *************** *************** **
      >[/color]



      It helps to post the actual code and error message you
      are seeing...


      [color=blue][color=green][color=darkred]
      >>> import datetime
      >>> class foo(datetime):[/color][/color][/color]
      .... pass
      ....
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      TypeError: function takes at most 2 arguments (3 given)[color=blue][color=green][color=darkred]
      >>> class foo(datetime.da tetime):[/color][/color][/color]
      .... pass
      ....[color=blue][color=green][color=darkred]
      >>> d = foo()[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      TypeError: function takes at least 3 arguments (0 given)[color=blue][color=green][color=darkred]
      >>> d = foo(1, 2, 3)
      >>> d[/color][/color][/color]
      foo(1, 2, 3, 0, 0)[color=blue][color=green][color=darkred]
      >>> datetime.dateti me(1, 2, 3)[/color][/color][/color]
      datetime.dateti me(1, 2, 3, 0, 0)



      My guess is that the code you posted is not really the
      code you were runnning.

      Comment

      • rafi

        #4
        Re: inheriting from datetime

        Rob Conner wrote:[color=blue]
        > So this is simple, why can't I run the following code? I've tried many
        > variances of this, but simply cannot inherit from datetime or
        > datetime.dateti me. I get this on line 3.
        > TypeError: function takes at most 2 arguments (3 given)[/color]

        line 3 of the following code or line 3 of the use of the code?

        on my box it runs, except that ValueError is not caught by the except.

        side question: what is the point of accepting invalid dates?

        --
        rafi

        "Imaginatio n is more important than knowledge."
        (Albert Einstein)

        Comment

        • Rob Conner

          #5
          Re: inheriting from datetime

          gah, yeah that was strange. but i got it now. thanks.
          [color=blue]
          > side question: what is the point of accepting invalid dates?[/color]
          thats a long story. but it would be nice to have invalid dates at least
          just stored. so i want to try to put a class together that does it.

          Comment

          Working...