Why doesn't this work?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Garret

    #1

    Why doesn't this work?

    Python 2.3.5 (#1, Jan 30 2006, 13:30:29)
    [GCC 3.3 20030304 (Apple Computer, Inc. build 1819)] on darwin
    Type "help", "copyright" , "credits" or "license" for more information.
    >>from datetime import datetime
    >>class ts(datetime):
    .... def __init__(self): pass
    ....
    >>ts()
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: function takes at least 3 arguments (0 given)
    >>>
  • Larry Bates

    #2
    Re: Why doesn't this work?

    Because datetime is a new-style class:

    The Constructor __new__

    If you are like me, then you probably always thought of the __init__ method as
    the Python equivalent of what is called a constructor in C++. This isn't the
    whole story.

    When an instance of a class is created, Python first calls the __new__ method of
    the class. __new__ is a static method that is called with the class as its first
    argument. __new__ returns a new instance of the class.

    The __init__ method is called afterwards to initialize the instance. In some
    situations (think "unplickling"!) , no initialization is performed. Also,
    immutable types like int and str are completely constructed by the __new__
    method; their __init__ method does nothing. This way, it is impossible to
    circumvent immutability by explicitly calling the __init__ method after
    construction.


    I think what you wanted was:
    >>class ts(datetime):
    .... def __new__(self): pass
    ....
    >>a=ts()
    -Larry

    Comment

    • Ron Garret

      #3
      Re: Why doesn't this work?

      In article <453A507E.40706 02@websafe.com> ,
      Larry Bates <larry.bates@we bsafe.comwrote:
      Because datetime is a new-style class:
      Ah.
      The Constructor __new__
      >
      If you are like me, then you probably always thought of the __init__ method
      as
      the Python equivalent of what is called a constructor in C++. This isn't the
      whole story.
      >
      When an instance of a class is created, Python first calls the __new__ method
      of
      the class. __new__ is a static method that is called with the class as its
      first
      argument. __new__ returns a new instance of the class.
      >
      The __init__ method is called afterwards to initialize the instance. In some
      situations (think "unplickling"!) , no initialization is performed. Also,
      immutable types like int and str are completely constructed by the __new__
      method; their __init__ method does nothing. This way, it is impossible to
      circumvent immutability by explicitly calling the __init__ method after
      construction.
      >
      >
      I think what you wanted was:
      >
      >class ts(datetime):
      ... def __new__(self): pass
      ...
      >a=ts()
      >
      -Larry
      Thanks!

      rg

      Comment

      Working...