[Tutor] modify values for object derived from datetime.datetime
rail shafigulin
rail.shafigulin at gmail.com
Thu Dec 15 23:01:28 CET 2011
i writing some code to do device testing at my work. testing is related to
date and time, so naturally i decided to create a class that inherits from
datetime.datetime. main reason is that i need to add, subtract and compare
datetime objects and datetime.datetime allows me to do that. here is the
code:
class LTime(datetime.datetime):
TOLERANCE = 10
def __new__(self, year, month, day, *args):
if year == 0:
year = 2000
return super().__new__(self, year, month, day, *args)
def modify(self):
self = self.replace(2012, 12, 12)
print(self)
def main():
mytime = LTime.now()
mytime.modify()
print(mytime)
if __name__ == '__main__':
main()
the problem that i see is that i'm not able to modify date and time because
it seems that those attributes are immutable. another problem that i see is
in the modify() routine. if you print mytime the date and time are still
old. can anybody explain why this is happening and if it is even possible
to achieve what i'm trying to achieve, which is to change self.date and
self.time
any help is appreciated.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111215/50d3b6bb/attachment-0001.html>
More information about the Tutor
mailing list