enthought / enthought/traits

__imul__ with a List of List is inconsistent with Python __imul__

Open
#962 10 comments 0 reactions 0 assignees View on GitHub
type: bug
Dominant language
Python
Stars
462
Forks
90
PR merge metrics
No merged PRs in 30d

Description

Consider this, where the list is an instance of `TraitListObject`:
```
from traits.api import HasTraits, List
class Foo(HasTraits):
a = List(List)

>>> foo = Foo(a=[[1, 2, 3], [3, 4, 5]])
>>> foo.a *= 2
[[1, 2, 3], [3, 4, 5], [1, 2, 3], [3, 4, 5]]
>>> foo.a
>>> foo.a[-1].append(6)
>>> foo.a
[[1, 2, 3], [3, 4, 5], [1, 2, 3], [3, 4, 5, 6]]
```
`TraitListObject.__imul__` creates copies for the "new" items.

For Python's list:
```
>>> a = [[1, 2, 3], [3, 4, 5]]
>>> a *= 2
[[1, 2, 3], [3, 4, 5], [1, 2, 3], [3, 4, 5]]
>>> a
>>> a[-1].append(6)
>>> a
[[1, 2, 3], [3, 4, 5, 6], [1, 2, 3], [3, 4, 5, 6]]
```
Note the difference in the second item in the final list. Python list `__imul__` simply repeats the items in the list.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.