[New-bugs-announce] [issue32194] When creating list of dictionaries and updating datetime objects one by one, all values are set to last one of the list.

Joona Mörsky report at bugs.python.org
Fri Dec 1 06:05:58 EST 2017


New submission from Joona Mörsky <joonamorskyer at gmail.com>:

When creating list of dictionaries and updating datetime objects one by one, all values are set to last one of the list.

Ubuntu Linux 4.10.0-40-generic #44~16.04.1-Ubuntu SMP Thu Nov 9 15:37:44 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
Python 3.5.2



>>>import datetime

>>>b = datetime.datetime.utcnow()
>>>b = b.replace(hour=0,minute=0,second=0,microsecond=0,tzinfo=datetime.timezone.utc)

>>>a = [{}] * 3

>>>for inx in range(3):

...    a[inx]['time'] = b + datetime.timedelta(minutes=inx*10)


[{'time': datetime.datetime(2017, 12, 1, 0, 20, tzinfo=datetime.timezone.utc)}, {'time': datetime.datetime(2017, 12, 1, 0, 20, tzinfo=datetime.timezone.utc)}, {'time': datetime.datetime(2017, 12, 1, 0, 20, tzinfo=datetime.timezone.utc)}]

>>>import datetime

>>>b = datetime.datetime.utcnow()
>>>b = b.replace(hour=0,minute=0,second=0,microsecond=0,tzinfo=datetime.timezone.utc)

>>>a = [ ]

>>>for inx in range(3):

...    a.append({"time": b + datetime.timedelta(minutes=inx*10)})

[{'time': datetime.datetime(2017, 12, 1, 0, 0, tzinfo=datetime.timezone.utc)}, {'time': datetime.datetime(2017, 12, 1, 0, 10, tzinfo=datetime.timezone.utc)}, {'time': datetime.datetime(2017, 12, 1, 0, 20, tzinfo=datetime.timezone.utc)}]

----------
components: Interpreter Core
messages: 307382
nosy: Joona Mörsky
priority: normal
severity: normal
status: open
title: When creating list of dictionaries and updating datetime objects one by one, all values are set to last one of the list.
type: behavior
versions: Python 3.5

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32194>
_______________________________________


More information about the New-bugs-announce mailing list