On 12/05/2015 23:44, Gollum1 wrote:
Il 12 maggio 2015 20:59:40 CEST, Daniele Zambelli <daniele.zambe...@gmail.com> 
ha scritto:

a = [1, 2, 3, 4]
b = [2, 3, 4, 5]
c = [1, 2, 3, 4]
a == b
False
a == c
True
  Mi pare di ricordare che questa il ultima non sia poi così scontata...

Quest'ultima e' senza dubbio vera (si confronta il valore, non l'identita')

cpython potrebbe semplificare le due liste con lo stesso oggetto, ma non è 
detto che lo faccia sempre è non è detto che lo facciano tutti gli 
interpreti... oppure ho detto una cavolata Marco Buttu?

CPython garantisce il contrario, ovvero che l'etichetta (il nome) `a` e l'etichetta `c` facciano riferimento a due oggetti (mutabili) distinti (diversa identita'). Questo e' sempre garantito per gli oggetti mutabili. Non e' garantito nel caso di oggetti immutabili [1]:


Types affect almost all aspects of object behavior. Even the importance of object identity is affected in some sense: for immutable types, operations that compute new values may actually return a reference to any existing object with the same type and value, while for mutable objects this is not allowed. E.g., after a = 1; b = 1, a and b may or may not refer to the same object with the value one, depending on the implementation, but after c = []; d = [], c and d are guaranteed to refer to two different, unique, newly created empty lists. (Note that c = d = [] assigns the same object to both c and d.)


[1] https://docs.python.org/3/reference/datamodel.html#objects-values-and-types

--
Marco Buttu

INAF-Osservatorio Astronomico di Cagliari
Via della Scienza n. 5, 09047 Selargius (CA)
Phone: 070 711 80 217
Email: mbu...@oa-cagliari.inaf.it

_______________________________________________
Python mailing list
Python@lists.python.it
http://lists.python.it/mailman/listinfo/python

Rispondere a