Basics

Object Lifecycle

Names keep objects reachable until the last reference goes away.

Two names can refer to the same object. Mutating through one name would affect the object seen through the other.

Source

class Box:
    def __init__(self, label):
        self.label = label

box = Box("draft")
alias = box

print(box is alias)
print(alias.label)

Output

True
draft
boxaliasBOXlabel='draft'last ref?
Names keep the same object reachable; deleting one name matters only when it removes the last reference.

Rebinding box does not change the original object. alias still reaches the first Box until that reference is removed too.

Source

box = Box("published")
print(alias.label)
print(box.label)

del alias
print("old object unreachable")

Output

draft
published
old object unreachable

Notes

See also

Run the complete example

Example code

Expected output

True
draft
draft
published
old object unreachable

Execution time appears here after you run the example.