+ 2

Can any one explain why this is happened

I assume that for every object their is new reference,but Why it is'nt right https://code.sololearn.com/cKQ5nKqvqK60/?ref=app

28th Dec 2018, 4:19 AM
Venkat
Venkat - avatar
4 Antworten
+ 3
For immutable objects, objects with the same value/content usually have the same id if they exist in overlapping periods. This hence, does not apply to lists, dicts, etc.
28th Dec 2018, 5:20 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 2
Two objects with non-overlapping lifetimes may have the same id() value. https://stackoverflow.com/questions/15667189/what-is-the-id-function-used-for
28th Dec 2018, 5:11 AM
Gordon
Gordon - avatar
+ 1
⏩ Prometheus ⏪ Is it means that python creates a unique id for a unique literal(immutable object)!
28th Dec 2018, 6:54 AM
Venkat
Venkat - avatar
0
If you write x = 5 y = x y will have the same id, but if later you write x = 7 y will remain 5, so now the one object has become two objects again. Since with immutable objects there's no risk that you change the same object via two different names without even knowing (like list.append(something)), there should be no harm 'summarizing' what the coder writes. You may write 'string' in your code ten times, but Python's like 'wait, we've got that already!', so you end up with just one 'string' with different name stickers. (And how and when exactly this is done - someone else will have to explain.)
28th Dec 2018, 10:51 AM
HonFu
HonFu - avatar