> Dear all, Ciao, credo che possiamo scrivere in italiano, giusto? ;)
Allora, la cosa che forse non è chiara, è che in python i nomi delle variabili sono semplici "riferimenti" a un oggetto, non corrispondono all'oggetto stesso. Esempio semplice che spero possa chiarire: >>> a = [1,2,3] >>> b = [1,2,3] >>> c = b >>> a is b False >>> b is c True > - shallow copy: produce a new object that is not linked to > the native object. Slicing on a list produces a shallow copy of the > original one. Quindi quando fai uno slice, quel che realmente fai non è la copia di tutti gli elementi uno per uno, ma copi _i_riferimenti_ dentro la lista.... gli oggetti rimangono gli stessi. tornando all'esempio di prima: >>> a[0] is b[0] True > - deep copy: bit-bit copy (in other words we have here a > reference to the native object (link)) questa non la chiamerei affatto 'deep copy', in quanto quel che fai è creare un'altro riferimento allo stesso oggetto. Nel tuo caso: l4 e l2 sono due nomi per la stessa lista, tant'è che se modifichi una delle due, anche l'altra rispecchierà le modifiche... solito esempio: >>> b.append(4) >>> c [1, 2, 3, 4] Quel che fa la funzione id() è ritornarti l'indentificatore all'oggetto (che quindi non c'entra con il nome con cui la chiami... ) >>> help(id) Return the identity of an object. This is guaranteed to be unique among simultaneously existing objects. (Hint: it's the object's memory address.) infatti: >>> id(b) == id(c) True > Therefore, is not clear to us why in list copy the shallow does not > affect the sub-list object. In other words the shallow copy is not > 'recursive' within a shollowed copied object. Esattamente. Lo slice non copia gli oggetti, copia i riferimenti che sono contenuti nella lista di cui fai slice. > More in general, we don't understand the behavior of the id() function. > Doesn't return the object identity (object adress memory)?? Si', ritorna esattamente quello. > Why the object identity of the three variables in the following example > is the same?? Does the id() function behave like the C/C++ > adress-operator &?? circa (come dice l'help....), non ne conosco esattamente l'implementazione, ma direi che puoi pensarlo come un puntatore all'oggetto in questione. Spero di essere stato d'aiuto. bye -- -gaspa- ----------------------------------------------- -------- https://launchpad.net/~gaspa --------- ------ HomePage: iogaspa.altervista.org ------- -Il lunedi'dell'arrampicatore: www.lunedi.org - _______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python