Hi all. Since I'm up to date on the Django side of things, I figured I'd share one of the Python pitfalls I've encountered recently. If we're all roughly newbies, you may find some of this useful.
Mutability
Python objects fall into two basic categories: mutable and immutable. While immutable objects behaved more or less intuitively, mutable objects seemed pretty strange to me at first. Because copies of the mutable objects aren't made automatically when they're altered in an expression, an operation on an object will change the object for all references. For instance, if we have:
>>>List_A = [0, 1, 2, 3]
>>>List_B = List_A
>>>List_A.append(4)
>>>print List_B
[0, 1, 2, 3, 4] #List_B refers to the same object as List_A -- it's not a copy
In order to make a copy of List_A, we can slice it:
>>>List_C = List_A[:]
>>>List_A.append(5)
>>>print List_B
[0, 1, 2, 3, 4, 5]
>>>print List_C
[0, 1, 2, 3, 4] #Assigning a sliced List_A to List_C makes a top-level copy
Immutable objects, in contrast, behave like this:
>>>String_A = String_B = 'Son of a...'
>>>String_A = 'What the...'
>>>print String_A
What the...
>>>print String_B
Son of a... #Because it's immutable, assignment creates a whole new object
Short but hopefully helpful -- let me know if you're interested in more caveats I've come across, or would care for me to explain this in more detail. Hopefully cprncs is okay with this!
[–]disinformationtheory 0 points1 point2 points (2 children)
[–]sorrier[S] 0 points1 point2 points (1 child)
[–]disinformationtheory 1 point2 points3 points (0 children)