#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    76
    Rep Power
    2

    Datetime/inheritance with instantiation.


    So, I'm trying to build a much more user-friendly datetime feature that lets me snag the current time, PROVIDED no arguments are passed when the class is instantiated. I'm also adding a more decorative description for it that'll allow me to build very effective timestamps for changelogs and some other stuff I've yet to think up, but that's besides the point.

    I have a lot of problems understanding super() and object inheritance, so please forgive me if this code is waaaaay off the mark.
    Code:
    import datetime
    
    class timestamp(datetime.datetime):
    	def __init__(self):
    		try:
    			super(timestamp, self).__init__()
    		except:
    			self = timestamp(datetime.datetime.now())
    So, does anyone have a way of doing this without just pulling every individual argument from datetime.datetime.now() as defaults?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    No one understands super. Wait!! Maybe Ray understands. He's written batches of python code and contributions to the python library. click here for Ray's explanation of super

    Now, try to figure out why this custom exception works.

    Code:
    class CustomException(Exception):
        pass
    
    raise CustomException('where did the code for this come from?')
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    76
    Rep Power
    2
    Yeah, I understand, the neat little print string is already inherited from the Exception class.
    What I don't understand is how to change default arguments for inheritance.
    Unless, of course, the only way is to create a constructor with all the redundant assignments, i.e.

    Code:
    class timestamp(datetime.datetime):
    	def __init__(self,year=datetime.now().year, etc.....):
    		self.year=year
    etc...
    I have an idea on HOW to do it, but I'm convinced there should be a way to do it much more cleanly.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    76
    Rep Power
    2
    I'm a little confused as to what, specifically, I'm supposed to be looking for there.
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    The class Base, inherited by a few other classes, supplies __init__ and __repr__ methods and, without explicitly saying so, __init__ requires certain keyword arguments which become object attributes. It's a way around writing supremely boring code like

    def __init__(self,this,that):self.this,self.that = this,that


    which may or may not address your complaint "So, does anyone have a way of doing this without just pulling every individual argument"

    and, if you read the code closely, you'll discover that I've directly accessed
    self.__dict__
    which again, might or might not be helpful. I agree that the spread of datetime into various modules at varying depths is a nuisance. I've recently started using the unix date command. It's terrific.

    date --date='2 days ago' +'%d/%m/%y'
    Last edited by b49P23TIvg; April 30th, 2013 at 12:10 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    76
    Rep Power
    2
    Okay. Now I think I'm more on track.

    And I don't know if it totally answers my original question. I guess I'll stay reliant on the pulling value-for-value method if a datetime.datetime is supplied, and otherwise just get the current values.

IMN logo majestic logo threadwatch logo seochat tools logo