Advanced Usage
In this step, we will explore some advanced usage of the is operator.
- Define a class
MyClass with an attribute value.
class MyClass:
def __init__(self, value):
self.value = value
- Define two instances of the
MyClass class, obj1 and obj2, with the same value.
obj1 = MyClass("Hello")
obj2 = MyClass("Hello")
- Print out the memory address of
obj1.value and obj2.value using the id() function.
print(id(obj1.value))
print(id(obj2.value))
- Use the
is operator to check if obj1.value and obj2.value refer to the same object.
print(obj1.value is obj2.value)
The output of the above code should be True, since the value attribute is an immutable object and both instances share the same object in memory.
- Now, define two instances of the
MyClass class, obj3 and obj4, with a mutable object as their value.
obj3 = MyClass([])
obj4 = MyClass([])
- Print out the memory address of
obj3.value and obj4.value using the id() function.
print(id(obj3.value))
print(id(obj4.value))
- Use the
is operator to check if obj3.value and obj4.value refer to the same object.
print(obj3.value is obj4.value)
The output of the above code should be False, since the value attribute is a mutable object and both instances do not share the same object in memory.