The following were what I did in python shell. Can anyone explain the difference?
datetime.datetime.now() datetime.datetime(2018, 9, 29, 21, 34, 10, 847635)
print(datetime.datetime.now()) 2018-09-29 21:34:26.900063
The first is the result of calling repr
on the datetime value, the second is the result of calling str
on a datetime.
The Python shell calls repr
on values other than None before printing them, while print
tries str
before calling repr
(if str
fails).
This is not dependent on the Python version.
When print is called, the inner __str__()
of class datetime
is called. This function calls isoformat()
and returns the date in ISO format.
def __str__(self):
"Convert to string, for str()."
return self.isoformat(sep=' ')
When an object's name is entered to the shell, __repr__()
is called and returns a formal representation of the object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.