On Fri, Dec 9, 2011 at 4:43 PM, rail shafigulin <rail.shafigu...@gmail.com>wrote:
> i need to overwrite and attribute from the inherited class. i also need to > run the constructor of the super class. here is the code > > import datetime > > class DateTime(datetime.datetime): > def __init__(self, year, month, day, *args): > super().__init__(year, month, day, *args) > if self.year >= 1000: > self.year = self.year % 1000 > > i'm getting the following error: > AttributeError: attribute 'year' of datetime.datetime objects are not > writable. > > some of you might suggest to change > > self.year = self.year % 1000 > to > self.yr = self.year % 1000 > > but i'd need to keep the name the same. > > any help is appreciated. > > my apologies, but there is a minor mistake in the code: import datetime class DateTime(datetime.datetime): def __init__(self, year, month, day, *args): super().__init__() if self.year >= 1000: self.year = self.year % 1000 def main(): mytme = DateTime.now() if __name__ == '__main__' main()
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor