Others constructors...

B

Batista, Facundo

Studying the module datetime, found that you can create a date instance
like:

mydate = datetime.date(2003, 9, 15)

but also you have others constructors. For example:

mydate = datetime.date.today()

How is this trick implemented in Python? (think the module is in C or
something, didn´t found a datetime.py).

Thanks for all.

Facundo Batista
Gestión de Red
(e-mail address removed)
(54 11) 5130-4643
Cel: 15 5132 0132







.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .
.. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . .
.. . . . . . . . . . . . . . .
ADVERTENCIA

La información contenida en este mensaje y cualquier archivo anexo al mismo,
son para uso exclusivo del destinatario y pueden contener información
confidencial o propietaria, cuya divulgación es sancionada por la ley.

Si Ud. No es uno de los destinatarios consignados o la persona responsable
de hacer llegar este mensaje a los destinatarios consignados, no está
autorizado a divulgar, copiar, distribuir o retener información (o parte de
ella) contenida en este mensaje. Por favor notifíquenos respondiendo al
remitente, borre el mensaje original y borre las copias (impresas o grabadas
en cualquier medio magnético) que pueda haber realizado del mismo.

Todas las opiniones contenidas en este mail son propias del autor del
mensaje y no necesariamente coinciden con las de Telefónica Comunicaciones
Personales S.A. o alguna empresa asociada.

Los mensajes electrónicos pueden ser alterados, motivo por el cual
Telefónica Comunicaciones Personales S.A. no aceptará ninguna obligación
cualquiera sea el resultante de este mensaje.

Muchas Gracias.
 
A

Andrew Dalke

Others constructors...Batista, Facundo:
but also you have others constructors. For example:
mydate = datetime.date.today()
How is this trick implemented in Python?

staticmethod, which is in newer Pythons.
.... def __init__(self, year, month, day):
.... self.year = year
.... self.month = month
.... self.day = day
.... def my_birthday():
.... return Date(1970, 8, 22)
.... my_birthday = staticmethod(my_birthday)
....
Andrew
(e-mail address removed)
 
P

Peter Otten

Studying the module datetime, found that you can create a date instance
like:

mydate = datetime.date(2003, 9, 15)

but also you have others constructors. For example:

mydate = datetime.date.today()

How is this trick implemented in Python? (think the module is in C or
something, didn?t found a datetime.py).

In addition to staticmethod, classmethod is also nice, as it can cope with
inheritance:

class Date:
def __init__(self, y, m, d):
self.ymd = y, m, d
def firstOfMonth(cls, y, m):
return cls(y, m, 1)
firstOfMonth = classmethod(firstOfMonth)

class PrintableDate(Date):
def __str__(self):
return "%04d-%02d-%02d" % self.ymd


print Date.firstOfMonth(1900, 1) # <__main__.Date instance at 0x402c36ec>
print PrintableDate.firstOfMonth(1900, 1) # 1900-01-01

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top