php yardım dosyasında şöyle bir ifade var:
Note: Also note that foreach operates on a copy of the specified array and not the array itself. Therefore, the array pointer is not modified as with the each() construct, and changes to the array element returned are not reflected in the original array. However, the internal pointer of the original array is advanced with the processing of the array. Assuming the foreach loop runs to completion, the array's internal pointer will be at the end of the array.
Ama benim anlamadığım madem nesne kopyalanıyor. neden id'leri aynı?
bunun için bir deneme yaptım:
Kod:
>>> import copy
>>> deneme = ['a', 'b', 'c']
>>> deneme_kopya = copy.copy(deneme);
>>> print deneme_kopya
['a', 'b', 'c']
>>> id(deneme[0])
-1209651808
>>> id(deneme_kopya[0])
-1209651808
evet kopyasınında id'si eşt oluyor nasıl yani dedim? o zaman birini değiştirince
biri de mi değişiyor?
Kod:
>>> deneme[0] = 'd'
>>> deneme
['d', 'b', 'c']
>>> deneme_kopya
['a', 'b', 'c']
hayır değişmiyordu. Sonra tekrar inceledim.
Kod:
>>> id(deneme[0])
-1209722720
>>> id(deneme_kopya[0])
-1209651808
Demekki python bir nesnenin kopyası yapıldığında değiştirilene kadar eski nesne
ile aynı adresi gösteriyormuş.
PHP manual ve python kabuğunun güzel bir sentezi ile sorunu çözmüş olduk.
Bookmarks