2 funny Py2.3 Issues: 'datetime', string.join()

Rami A. Kishek ramiak2000 at yahoo.com
Wed Aug 20 00:57:01 CEST 2003

1) I particular like how the Python 2 syntax has all the string
functions as methods of the <str> object (so we don't have to import
string.  However, wouldn't it make more sense if the "join" mehtod
actually belonged to the List object, since it primarily operates on
 e.g., supposing we want to do 
   string.join(string.split(a, '\n'))

 This now translates either into
 (which still requires us  to import string, or into the crude:
 which is very confusing for anyone browsing the code.

 If join() was attached to lists however, this would read much more
nicely as:

2) I had a difficult time trying to subclass the new datetime.date
class.  I basically had written something similar for my own
applications, and now want to convert my class to take advantage of the
'datetime.date' class.  Unfortunately, seems there is no way I can
override some of the methods provided by the date object.  E.g.,
supposing I wish to have the option of initializing the class using a
tuple(y,m,d) or a string date instead of the 3 integers.
I can build my __init__() to test the type of the first argument and act
accordingly, e.g.:

    class RDate(datetime.date):
        def __init__(self, year, month=1, day=1):
            if type(year) == type(()): year, month, day = year[0:2]
            datetime.date.__init__(self, year, month, day)

Unfortunately, Python doesn't like that, and keeps complaining when I
pass a tuple:

>>> RDate((2003,5,6))
TypeError: function takes exactly 3 arguments (1 given)

If instead I try to forget about datetime.date.__init__() and set my own
vars (like self.year = year, etc.), I get:

    self.year, self.month, self.day = (year, month, day)
TypeError: attribute 'year' of 'datetime.date' objects is not writable

Anyone has any clue what is going on here?

More information about the Python-list mailing list