Subclassing: what is wrong here?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paulo da Silva

    #1

    Subclassing: what is wrong here?

    Hi!

    What's wrong with this way of subclassing?

    from datetime import date

    class MyDate(date):
    def __init__(self,y ear,month=None, day=None):
    if type(year) is str:
    # The whole date is here as a string
    year,month,day= map(int,string. split(year,'-'))
    if year<100:
    year+=2000
    date.__init__(s elf,year,month, day)

    When I do
    d=MyDate("2007-3-15")
    I got
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: function takes exactly 3 arguments (1 given)

    Thanks for any help.
  • Miki

    #2
    Re: Subclassing: what is wrong here?

    Hello Paulo,
    What's wrong with this way of subclassing?
    ...
    See http://sourceforge.net/tracker/index...70&atid=105470

    HTH,
    --
    Miki <miki.tebeka@gm ail.com>
    If it won't be simple, it simply won't be. [Hire me, source code]


    Comment

    • Paulo da Silva

      #3
      Re: Subclassing: what is wrong here?

      Miki escreveu:
      Hello Paulo,
      >
      >What's wrong with this way of subclassing?
      >...
      See http://sourceforge.net/tracker/index...70&atid=105470
      >
      HTH,
      --
      Miki <miki.tebeka@gm ail.com>
      If it won't be simple, it simply won't be. [Hire me, source code]

      >
      Thanks!
      It works this way:

      class MyDate(date):
      def __new__(cls,yea r,month=None,da y=None):
      if type(year) is str:
      # The whole date is here as a string
      year,month,day= map(int,string. split(year,'-'))
      if year<100:
      year+=2000
      return date.__new__(cl s,year,month,da y)

      Comment

      Working...