Let's say I have a python class (implementation is irrelevant), say it's class "A" as such:
>>> class A:
... pass
...
Then for some reason, I need a list of 3 instance of class "A". I use the following syntax (multiplication operator) to create it:
>>> some_a = [A(),] * 3
>>> len(some_a)
3
Great! Looks like we have and array of tree instances of "A". But if we print the some_a
array, it appears (looking at the object address) that we have 3 reference to the same instance:
>>> repr(some_a)
'[<__main__.A object at 0x7fc6fec7e290>, <__main__.A object at 0x7fc6fec7e290>, <__main__.A object at 0x7fc6fec7e290>]'
Why it the behavior as such ? What it the rational behind it ?
I was not able to find the documentation of the multiplication operator effect on lists in the python language reference, can someone point me to it ?