python - How to create and compare datetimes in custom class? -
i'm writing __init__
, __eq__
functions class photo
, involving datetime
module. however, i'm unsure of __init__
function body i've written , how test __eq__
.
this have __init__
function:
class photo: 'fields: size, pdate' # purpose: constructor class photo # __init__: int int int int -> photo # note: function definition needs self parameter , not require return statement def __init__(self, size, year, month, day): self.size = size self.pdate = year + month + day
i think self.pdate
wrong i'm not sure i'm supposed write instead. perhaps following?
self.pdate = year self.date = month self.date = day
from documentation of datetime module, can create datetime.date
objects using following:
from datetime import date some_random_date = date(2013, 7, 28) not_so_random = date.today()
for use case, kind of objects want affect self.pdate
attribute:
from datetime import date class photo: 'fields: size, pdate' # purpose: constructor class photo # __init__: int int int int -> photo # note: function definition needs self parameter , not require return statement def __init__(self, size, year, month, day): self.size = size self.pdate = date(year, month, day)
and in order compare 2 of objects:
def __eq__(self, other): # test other photo, left exercise return self.size == other.size , self.pdate == other.pdate def __ne__(self, other): return self.size != other.size or self.pdate != other.pdate
Comments
Post a Comment