[Tutor] attribute overwrite

rail shafigulin rail.shafigulin at gmail.com
Fri Dec 9 23:14:44 CET 2011


On Fri, Dec 9, 2011 at 4:43 PM, rail shafigulin
<rail.shafigulin at 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()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111209/dee604fa/attachment.html>


More information about the Tutor mailing list