I was using the datetime module for the first time today. When I typed

now = datetime.date.today()
now

I got the output

datetime.date(2009, 12, 27)

Now, I found out that

datetime.date

is a class and that

datetime.date.today

is a method. But I can't figure out what the output means.
First it gives the class datetime.date followed by what seems to be the parameters of an object of class datetime.date. "(2009, 12, 27)"

What kind of method would output the class and the parameters of one of it's objects like this? I'd appreciate an example. Here's what I did to try to figure it out. It might help.

>>> import datetime
>>> now = datetime.date.today()
>>> type (now)
<class 'datetime.date'>
>>> 
>>> 
>>> datetime.date
<class 'datetime.date'>
>>> 
>>> 
>>> datetime.date.today
<built-in method today of type object at 0x825daa0>
>>>

(This was copied from a command line session of python)

Consider this

>>> import datetime
>>> value = datetime.date.today()
>>> print(value)
2009-12-27
>>> value
datetime.date(2009, 12, 27)

When you call print(value) , the string printed is the string resulting from the call str(value) , or equivalently, the string returned by the method __str__() of the class. On the other hand, when the interpreter displays the value (without print), the string actually printed is the string resulting from the call repr(value) , or equivalently the string returned by the method __repr__ . The rule in python, is that when this is possible, the __repr__ method should return an expression which, if evaluated, would allow to reconstruct a new copy of the object. The datetime.date class follows this rule. Many classes don't because they contain too complex data or because their author neglected this feature.

commented: Informative as usual +1

You are getting confused by the Python shell results.

The Python shell is there to quickly explore some Python code lines, not to write working Python programs.

commented: This post confused me even more.. +0

Consider this

>>> import datetime
>>> value = datetime.date.today()
>>> print(value)
2009-12-27
>>> value
datetime.date(2009, 12, 27)

When you call print(value) , the string printed is the string resulting from the call str(value) , or equivalently, the string returned by the method __str__() of the class. On the other hand, when the interpreter displays the value (without print), the string actually printed is the string resulting from the call repr(value) , or equivalently the string returned by the method __repr__ . The rule in python, is that when this is possible, the __repr__ method should return an expression which, if evaluated, would allow to reconstruct a new copy of the object. The datetime.date class follows this rule. Many classes don't because they contain too complex data or because their author neglected this feature.

So how do statements like value.date and value.year work?
EDIT: whoops.. I think I understand. A is just an object of the .date class which has it's own fields. However, just typing 'a' at the interpreter prompt will make python display information about the object in a particular way. (i.e using the _repr_ method of the object). Am I right?

Here is an example ...

# example of methods you can override/overload in classes
# __str__() overloads str() as applied to the class instance
# note that print or print() uses str()

class Dollars(object):
    def __init__(self, amount):
        self.amount = amount
        
    def __str__(self):
        return "You owe me $%s" % str(self.amount)
    
a = 10
print( a )  # 10

# create a class instance
dollar = Dollars(10)
print( dollar )  # You owe me $10

A coulpe of class that show __str__ and __repr__ methods.

import datetime

class Time(object):
    def __init__(self,date):
        self.date = date
        
    def __str__(self):
        """Return a pleasant representation."""
        return 'Return a human-readable string: %s' % (self.date)    
    

t = Time(datetime.date.today())
print t  #The __str__ method of a class is called whenever Python needs a string representation of an object
'''
Return a human-readable string: 2009-12-27
'''
class Time(object):
    def __init__(self,date):
        self.date = date       
        
    def __repr__(self):
        '''We use __repr__ to produce a clear definition of how to recreate the given object'''
        return 'Return an expression: %r' % (self.date)    

t = Time(datetime.date.today())
print t
'''
Return an expression: datetime.date(2009, 12, 27)
'''

Navigate and getting help

>>> dir(datetime)
['MAXYEAR', 'MINYEAR', '__doc__', '__name__', '__package__', 'date', 'datetime', 'datetime_CAPI', 'time', 'timedelta', 'tzinfo']
>>> dir(datetime.date)
['__add__', '__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__', '__new__', '__radd__', '__reduce__', '__reduce_ex__', '__repr__', '__rsub__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', 'ctime', 'day', 'fromordinal', 'fromtimestamp', 'isocalendar', 'isoformat', 'isoweekday', 'max', 'min', 'month', 'replace', 'resolution', 'strftime', 'timetuple', 'today', 'toordinal', 'weekday', 'year']
>>> dir(datetime.date.today)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> #There are no more methods only special method that get called when you use the class

>>> #we look at __doc__
>>> print datetime.date.today.__doc__
Current date or datetime:  same as self.__class__.fromtimestamp(time.time()).
>>> #Or you can use help

>>> help(datetime.date.today)
Help on built-in function today:

today(...)
    Current date or datetime:  same as self.__class__.fromtimestamp(time.time()).
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.