1586847882
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
1619518440
Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.
…
#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners
1619510796
Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.
Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is
Syntax: x = lambda arguments : expression
Now i will show you some python lambda function examples:
#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map
1597751700
Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc…
You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like init, call, str etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).
Now there are a number of such special methods, which you might have come across too, in Python. We will just be taking an example of a few of them to understand how they work and how we can use them.
class AnyClass:
def __init__():
print("Init called on its own")
obj = AnyClass()
The first example is _init, _and as the name suggests, it is used for initializing objects. Init method is called on its own, ie. whenever an object is created for the class, the init method is called on its own.
The output of the above code will be given below. Note how we did not call the init method and it got invoked as we created an object for class AnyClass.
Init called on its own
Let’s move to some other example, add gives us the ability to access the built in syntax feature of the character +. Let’s see how,
class AnyClass:
def __init__(self, var):
self.some_var = var
def __add__(self, other_obj):
print("Calling the add method")
return self.some_var + other_obj.some_var
obj1 = AnyClass(5)
obj2 = AnyClass(6)
obj1 + obj2
#python3 #python #python-programming #python-web-development #python-tutorials #python-top-story #python-tips #learn-python
1602968400
Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler?
In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.
Swapping value in Python
Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead
>>> FirstName = "kalebu"
>>> LastName = "Jordan"
>>> FirstName, LastName = LastName, FirstName
>>> print(FirstName, LastName)
('Jordan', 'kalebu')
#python #python-programming #python3 #python-tutorials #learn-python #python-tips #python-skills #python-development
1602666000
Today you’re going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates.
In many situations you may find yourself having duplicates files on your disk and but when it comes to tracking and checking them manually it can tedious.
Heres a solution
Instead of tracking throughout your disk to see if there is a duplicate, you can automate the process using coding, by writing a program to recursively track through the disk and remove all the found duplicates and that’s what this article is about.
But How do we do it?
If we were to read the whole file and then compare it to the rest of the files recursively through the given directory it will take a very long time, then how do we do it?
The answer is hashing, with hashing can generate a given string of letters and numbers which act as the identity of a given file and if we find any other file with the same identity we gonna delete it.
There’s a variety of hashing algorithms out there such as
#python-programming #python-tutorials #learn-python #python-project #python3 #python #python-skills #python-tips