Writing an immutable object in python
Donn Cave
donn at u.washington.edu
Mon Oct 17 12:58:17 EDT 2005
In article <1129562898.812915.238430 at g14g2000cwa.googlegroups.com>,
"Mapisto" <mapisto at gmail.com> wrote:
> I've noticed that if I initialize list of integers in the next manner:
>
> >>> my_list = [0] * 30
>
> It works just fine, even if I'll try to assign one element:
>
> >>> id( my_list[4] )
> 10900116
> >>> id( my_list[6] )
> 10900116
> >>> my_list[4] = 6
> >>> id( my_list[4] )
> 10900044
> >>> id( my_list[6] )
> 10900116
>
> The change in the poision occurs becouse int() is an immutable object.
>
> if I will do the same with a user-defined object, This reference
> manipulating will not happen. So, every entry in the array will refer
> to the same instance.
Not at all. If you do the same thing,
class C:
pass
c = C()
a = [c]*12
... etc., you should observe the same pattern with respect to
object identities. Mutability doesn't really play any role here.
> Is there a way to bypass it (or perhaps to write a self-defined
> immutable object)?
Bypass what? What do you need?
Donn Cave, donn at u.washington.edu
More information about the Python-list
mailing list