defaultdict and datetime

Jason Friedman jsf80238 at gmail.com
Sat Aug 18 11:43:40 EDT 2018


$ python3
Python 3.6.1 (default, Apr  8 2017, 09:56:20)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import collections, datetime
>>> x = collections.defaultdict(int)
>>> x['something']
0
>>> x = collections.defaultdict(datetime.datetime)
>>> x['something']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Required argument 'year' (pos 1) not found

I would like to have a dictionary where the default value is a
datetime.datetime object, preferably something small like January 1, year 1.



More information about the Python-list mailing list