3 ms·
It's really not that complex, but it does have to be learned. Everything in Python is an object with an id. Assignment and parameter passing is by object id.
by mypalmike 3y ago
It's really not that complex, but it does have to be learned.
Everything in Python is an object with an id. Assignment and parameter passing is by object id.
There are some mutable objects ("objects", lists, dicts) and some immutable objects (numbers, strings, tuples).
The []*n syntax is treated as assignment, hence the id is duplicated. This isn't an issue with immutable objects since there's no way to change the underlying object - you can only change the id of each object in the list. Obviously there are some gotchas like with mutable objects here where the succinct syntax can be confusing. But I think the simplicity of everything being an id has deep intuitive value and is one reason Python has been so successful, even if many users never really think much about ids.