Home Tutorials Tutorial on Identity Operators in MicroPython

Tutorial on Identity Operators in MicroPython

by shedboy71

In MicroPython, identity operators are used to compare the memory locations of two objects, to check if they refer to the same object or not.

Identity operators are particularly useful when you want to check if two variables are pointing to the exact same object in memory.

This tutorial will cover the identity operators available in MicroPython, along with practical examples to demonstrate their use.

Table of Contents:

1. Overview of Identity Operators

MicroPython supports two identity operators:

Operator Description Example
is Returns True if two variables point to the same object in memory a is b
is not Returns True if two variables do not point to the same object in memory a is not b

These operators are different from the equality operators (==, !=), which check if the values of two objects are the same, whereas identity operators check if the objects themselves are the same.

2. is Operator

The is operator checks if two variables refer to the same object in memory. It returns True if they are the same object and False otherwise.

Syntax:

result = a is b

Example:

a = [1, 2, 3]
b = a  # Both a and b refer to the same object

result = a is b
print(result)  # Output: True

Explanation:

Since b is assigned the same list as a, both variables refer to the same object in memory, so a is b returns True.

3. is not Operator

The is not operator checks if two variables refer to different objects in memory. It returns True if they are different objects and False if they are the same object.

Syntax:

result = a is not b

Example:

a = [1, 2, 3]
b = [1, 2, 3]  # b is a different object with the same value as a

result = a is not b
print(result)  # Output: True

Explanation:

Although a and b contain the same elements, they are different objects stored at different memory locations, so a is not b returns True.

4. Comparing Immutable and Mutable Objects

Identity operators behave differently when used with immutable objects (e.g., integers, strings, tuples) compared to mutable objects (e.g., lists, dictionaries, sets).

Example with Immutable Objects:

a = 10
b = 10

result = a is b
print(result)  # Output: True

Explanation:

For small integers and strings, MicroPython optimizes memory usage by reusing the same object for identical values. Hence, a is b returns True because both refer to the same memory location.
Example with Mutable Objects:

a = [1, 2, 3]
b = [1, 2, 3]

result = a is b
print(result)  # Output: False

Explanation:

In this case, even though a and b have the same elements, they are separate objects in memory. Therefore, a is b returns False.

5. Difference Between == and is

== compares values: It checks if two objects have the same value.
is compares identity: It checks if two objects are the same instance in memory.

Example 1: Using ==

a = [1, 2, 3]
b = [1, 2, 3]

result = a == b
print(result)  # Output: True

Explanation:

Here, a == b returns True because the values of the lists are the same, even though they are different objects.

Example 2: Using is

a = [1, 2, 3]
b = [1, 2, 3]

result = a is b
print(result)  # Output: False

Explanation:

The is operator returns False because a and b are different objects in memory, even though their values are the same.

6. Practical Examples

Example 1: Checking Identity for Strings

x = "hello"
y = "hello"

# Checking if x and y are the same object in memory
print(x is y)  # Output: True

Explanation:

Strings are immutable, and MicroPython optimizes memory by reusing objects for identical strings. Hence, x is y returns True.

Example 2: Checking Identity for Lists

x = [1, 2, 3]
y = [1, 2, 3]

# Checking if x and y are the same object in memory
print(x is y)  # Output: False

Explanation:

Lists are mutable, so even though x and y have the same values, they are different objects in memory. Hence, x is y returns False.

Example 3: Modifying Objects Referenced by Multiple Variables

a = [1, 2, 3]
b = a  # Both variables refer to the same object

b.append(4)
print(a)  # Output: [1, 2, 3, 4]
print(b)  # Output: [1, 2, 3, 4]

Explanation:

Since b refers to the same object as a, any modification to b will also affect a.

Example 4: Using is not to Check Identity

a = [1, 2, 3]
b = [1, 2, 3]

# Checking if a and b are different objects
if a is not b:
    print("a and b are different objects")

Output:

a and b are different objects

Explanation:

Even though a and b contain the same values, they are different objects in memory, so a is not b returns True.

Conclusion

Identity operators (is and is not) in MicroPython allow you to check if two variables refer to the same object in memory.

They are especially useful when working with mutable objects or when you need to know whether two variables point to the same instance. Here’s a summary of what you learned:

is checks if two variables refer to the same object.
is not checks if two variables refer to different objects.
Immutable objects (like strings and integers) may share memory when their values are identical.
Mutable objects (like lists and dictionaries) are always stored at different memory locations, even if their values are identical.

Understanding the difference between identity operators and equality operators is crucial when working with object references, especially in the context of embedded systems programming.

You may also like

Leave a Comment