Day 8: Identity, Type & Value – a coming of age story about objects.

Everything from a string to a class is an object in Python. And every Object is defined by Identity, Type and Value.

 

Identity – refers to the location of the object in the computers memory (ie. id: 4358658568)

Type – is it a number or a string or a function? (possible types in Python include: str, int, float, list, tuple, dict, class, type, function)

Value – is the value of the object (ie. a string could be “Hello” or a number could be 3.14)

 

The id or the memory-location is not particularly useful in Python compared to other languages where you might have to

free the memory manually. When a Python program exits it clears all the used memory by itself – Python is a good sport so to speak.

Memory Management in other languages can be a bit of a pain in the elbow.

 

But you can learn a lot about the lifespan and behavior of objects such as strings when you know their ID –  just look at the following code:

 

name = "robb"       # name gets a value "robb"

name2 = name        # name2 gets the value of name which is "robb"

# even though they are two different variables, they share the same id or memory slot
print(id(name))     # id: 4374158280
print(id(name2))    # id: 4374158280

name2 = "adrian"    # change name2 to "adrian"

# the id of name stays the same, while name2 gets a new id as expected
print(id(name))     # id: 4374158280
print(id(name2))    # id: 4375412952

name = name2        # change name to the value of name2, which is "adrian"

#they both have the same id again 
print(id(name))     # id: 4375412952
print(id(name2))    # id: 4375412952

name = "robb"       # changing name back to its original value

print(id(name))     # id: 4374158280 <--It's the same ID again as it was in the first place (line 6) - crazy!
print(id(name2))    # id: 4375412952

I would have assumed that name gets a new id in the end, since we re-change the value to “robb”
but Python apparently remembers “Aha – we had that value before and gives the id of the memory
location back to the name-variable – it’s a neat way of managing memory space…

 

The above only works for immutable types though – a new or changed value changes the id.
(Immutable types are strings, integers, floats and tuples.)

 

Mutables types on the other hand, like lists or dictionaries, can change their value and
will still keep their original id. Just look at this:

 

a_list = ["robb", "adrian"] # we generate a list that holds two values

print(id(a_list))           # id: 4383824392

a_list.append("linus")      # we include another value to the list

print(id(a_list))           # id: 4383824392 <-- the value stays the same

del a_list[:]               # clear the list of all values

a_list.append("sara")       # enter a new value

print(id(a_list))           # id: 4383824392 <-- still the same.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.