There are over 70 built-in functions that are available to any Python interpreter when it is launched. Every Python learner shouldn’t be unfamiliar with some common ones. For example, we can use len()
to get the length of an object, such as the number of items in a list or dictionary. For another example, we can use print()
to print out the objects of interest for learning and debugging purposes.
Besides, almost all Python programmers should’ve seen the use of the built-in id()
function in tutorials for instructional purposes to demonstrate specific Python concepts. However, to my knowledge, these pieces of information are scattered. In this article, I would like to provide a systematic review of using the id()
function to understand six key Python concepts.
As a popular object-oriented programming language, Python uses objects everywhere in its implementations. For example, built-in data types such as integers, floating-point numbers, strings, lists, and dictionaries are all objects. Moreover, functions, classes, and even modules are used as objects too.
By definition, the id()
function takes an object and returns the object’s identity, which is the memory address expressed as an integer. Thus, we can use this function to show that it’s indeed true that everything is an object in Python.
>>> import sys
>>> class Foo:
... pass
...
>>> def foo():
... pass
...
>>> a_tuple = ('Error', 404)
>>> a_dict = {'error_code': 404}
>>> a_list = [1, 2, 3]
>>> a_set = set([2, 3, 5])
>>> objects = [2, 2.2, 'hello', a_tuple, a_dict, a_list, a_set, Foo, foo, sys]
>>>
>>> for item in objects:
... print(f'{type(item)} with id: {id(item)}')
...
<class 'int'> with id: 4479354032
<class 'float'> with id: 4481286448
<class 'str'> with id: 4483233904
<class 'tuple'> with id: 4483061152
<class 'dict'> with id: 4483236000
<class 'list'> with id: 4483236720
<class 'set'> with id: 4483128688
<class 'type'> with id: 140235151304256
<class 'function'> with id: 4483031840
<class 'module'> with id: 4480703856
Python objects and the id() function.
In the code snippet above, you can see that every item in the objects
list can be used in the id()
function, which reveals the memory address for each object.
The following operation, which I think is pretty interesting, is that as a function itself, the id()
function should have its memory address too.
>>> print(f'{type(id)} with id: {id(id)}')
<class 'builtin_function_or_method'> with id: 4480774224
When we create a variable in Python, we typically use the following syntax:
var_name = the_object
This process basically binds the object, which is created in the memory, to the particular variable name. What will happen if we assign a variable with another variable, like var_name1 = var_name
?
Consider the following example. In the code snippet below, we first created a variable called hello
to which we assigned a string value. Next, we created another variable called world
by assigning the previous variable hello
. When we printed out their memory addresses, we found that both hello
and world
have the same memory address, suggesting that they’re the same object in the memory.
>>> hello = 'Hello World!'
>>> print(f'{hello} from: {id(hello)}')
Hello World! from: 4341735856
>>> world = hello
>>> print(f'{world} from: {id(world)}')
Hello World! from: 4341735856
>>>
>>> bored = {'a': 0, 'b': 1}
>>> print(f'{bored} from: {id(bored)}')
{'a': 0, 'b': 1} from: 4341577200
>>> more_bored = bored
>>> print(f'{more_bored} from: {id(more_bored)}')
{'a': 0, 'b': 1} from: 4341577200
>>> more_bored['c'] = 2
>>> bored
{'a': 0, 'b': 1, 'c': 2}
>>> more_bored
{'a': 0, 'b': 1, 'c': 2}
Variable assignment and aliasing.
In this case, the variable world
is typically termed an alias of the variable hello
, and the process of creating a new variable by assigning an existing variable can be referred to as aliasing. In other programming languages, aliases are pretty much like pointers or references in relation to the underlying objects in the memory.
In the code above, we can further see that when we created an alias for a dictionary and modified the alias’s data, the modification also applied to the original variable because, under the hood, we modified the same dictionary object in the memory.
In various scenarios, we need to compare two objects as a decision-making point to apply different functionalities when a particular condition is met or not. In terms of equality comparisons, we can use two comparison operators: ==
and is
. Some new Python learners may mistakenly think they’re the same, but there is a nuance.
Consider the following examples. We created two lists of the same items. When we compared the two lists using the ==
operator, the comparison result was True
. When we compared the two lists using the is
operator, the comparison result was False
. Why did they produce different results? It’s because the ==
operator compares the values, while the is
operator compares the identities (i.e. memory addresses).
As you can expect for the variables that reference the same object in the memory, they don’t only have the same values but also have the same identities. This results in the evaluation results being the same for both the ==
and is
operators, as shown in the example below involving str0
and str1
:
>>> list0 = [1, 2, 3, 4]
>>> list1 = [1, 2, 3, 4]
>>> print(f'list0 == list1: {list0 == list1}')
list0 == list1: True
>>> print(f'list0 is list1: {list0 is list1}')
list0 is list1: False
>>> print(f'list0 id: {id(list0)}')
list0 id: 4341753408
>>> print(f'list1 id: {id(list1)}')
list1 id: 4341884240
>>>
>>> str0 = 'Hello'
>>> str1 = str0
>>> print(f'str0 == str1: {str0 == str1}')
str0 == str1: True
>>> print(f'str0 is str1: {str0 is str1}')
str0 is str1: True
>>> print(f'str0 id: {id(str0)}')
str0 id: 4341981808
>>> print(f'str1 id: {id(str1)}')
str1 id: 4341981808
Comparison operators.
One group of data that we use frequently in programming is integers. In Python, interpreters will typically cache small integers in the range of -5 to 256. It means that when the Python interpreter is launched, these integers will be created and available for later use in the memory. The following code snippet shows this feature:
>>> number_range = range(-10, 265)
>>> id_counters = {x: 0 for x in number_range}
>>> id_records = {x: 0 for x in number_range}
>>>
>>> for _ in range(1000):
... for number in number_range:
... id_number = id(number)
... if id_records[number] != id_number:
... id_records[number] = id_number
... id_counters[number] += 1
...
>>> [x for x in id_counters.keys() if id_counters[x] > 1]
[-10, -9, -8, -7, -6, 257, 258, 259, 260, 261, 262, 263, 264]
Integer caching.
In the code above, I created two dictionaries with id_counters
tracking the counts of unique identities for each integer and id_records
tracking the latest identity of the integer. For the integers in the range of -10 to 265, if the new integer’s identity is different from the existing one, the corresponding counter increments by one. I repeated this process 1,000 times.
The last line of code used the list comprehension technique to show you the integers that have more than one identity. Evidently, after 1,000 times, the integers from -5 to 256 had only one identity for each, as discussed in the last paragraph. To learn more about Python’s list comprehension, you can refer to my previous article on this:
From time to time, we need to make copies of existing objects such that we can change one copy without changing the other. The built-in copy
module provides two methods for this purpose: copy()
and deepcopy()
, which make shallow and deep copies, respectively. If you don’t know what they are, let’s leverage the id()
function to understand these two concepts.
>>> import copy
>>> original = [[0, 1], 2, 3]
>>> print(f'{original} id: {id(original)}, embeded list id: {id(original[0])}')
[[0, 1], 2, 3] id: 4342107584, embeded list id: 4342106784
>>> copy0 = copy.copy(original)
>>> print(f'{copy0} id: {id(copy0)}, embeded list id: {id(copy0[0])}')
[[0, 1], 2, 3] id: 4341939968, embeded list id: 4342106784
>>> copy1 = copy.deepcopy(original)
>>> print(f'{copy1} id: {id(copy1)}, embeded list id: {id(copy1[0])}')
[[0, 1], 2, 3] id: 4341948160, embeded list id: 4342107664
Shallow and deep copies.
We first created a list variable called original
, which consisted of a nested list and two integers. We then made two copies (copy0
and copy1
) using the copy()
and deepcopy()
methods, respectively. As we can expect, the original
, copy0
, and copy1
have the same values (i.e. [[0, 1], 2, 3]
). However, they have distinct identities because, unlike aliasing, both the copy()
and deepcopy()
methods do create new objects in the memory such that new copies have different identities.
The most essential difference between shallow and deep copies is that deep copying will create copies recursively for the original compound objects, while shallow copying will maintain the references to existing objects if applicable. In the example shown above, the variable original
is actually a compound object (i.e. a list is nested in another list).
In this case, using the copy()
method, the variable copy0
’s first element has the same identity (i.e. the same object) as the original
’s first element. By contrast, the deepcopy()
method makes a copy of the nested list in the memory such that the first element in copy1
has a different identity from the original
’s.
But what does “recursively” mean in deep copying? It means if there are multiple layers of nesting (e.g. a list nested in a list, which is nested in another list), the deepcopy()
method will make new objects for each layer. See the following example for this feature:
>>> mul_nested = [[[0, 1], 2], 3]
>>> print(f'{mul_nested} id: {id(mul_nested)}, inner id: {id(mul_nested[0])}, innermost id: {id(mul_nested[0][0])}')
[[[0, 1], 2], 3] id: 4342107824, inner id: 4342106944, innermost id: 4342107424
>>> mul_nested_dc = copy.deepcopy(mul_nested)
>>> print(f'{mul_nested_dc} id: {id(mul_nested_dc)}, inner id: {id(mul_nested_dc[0])}, innermost id: {id(mul_nested_dc[0][0])}')
[[[0, 1], 2], 3] id: 4342107264, inner id: 4342107984, innermost id: 4342107904
Recursive in deepcopy().
One advanced topic in Python programming pertains to data mutability. Generally speaking, immutable data are those objects whose values can’t be changed after their creation, such as integers, strings, and tuples. By contrast, mutable data refers to those objects whose values can be changed after their creation, such as lists, dictionaries, and sets.
One crucial thing to note is that by “changing the values,” we mean whether the underlying object in the memory can be changed or not. A thorough discussion of data mutability can be found in my previous article:
For the purpose of this article’s discussion of the id()
function, let’s consider the following example. For the immutable data type (the integer variable thousand
in the code snippet), when we tried to change its value, a new integer was created in the memory, as reflected by the new identity of the thousand
variable. In other words, the original underlying integer object couldn’t be changed. The attempt to change the integer just resulted in a new object created in the memory.
>>> thousand = 1000
>>> print(f'{thousand} id: {id(thousand)}')
1000 id: 4342004944
>>> thousand += 1
>>> print(f'{thousand} id: {id(thousand)}')
1001 id: 4342004912
>>> numbers = [4, 3, 2]
>>> print(f'{numbers} id: {id(numbers)}')
[4, 3, 2] id: 4342124624
>>> numbers += [1]
>>> print(f'{numbers} id: {id(numbers)}')
[4, 3, 2, 1] id: 4342124624
Data mutability and id().
If this is confusing to you, let’s see what happened to a mutable data type — in our case, the list
variable numbers
. As shown in the code above, when we tried to change the numbers
’s values, the variable numbers
got updated and the updated list still had the same identity, confirming the mutability of the objects of the list
type.
In this article, we leveraged the built-in id()
function to understand six key concepts in Python. Here is a quick recap of these concepts:
==
compares values, while the comparison operator is
compares identities.Thank you!
#python #programming #artificial intelligence #technology