GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Shared Reference in Python - GeeksforGeeks Shared Reference in PythonLast Updated : 27 Jul, 2026 A shared reference occurs when multiple variables refer to the same object in memory. Instead of creating a new object during assignment, Python creates another reference to the existing object. Shared references are also needed because changes made to mutable objects, such as lists and dictionaries, can affect all variables that reference the same object. Example:
Output 5 5 Reassigning VariablesReassigning a variable creates a new reference without affecting other variables.
Output Geeks 5 Explanation:
If an object is no longer referenced, Python eventually reclaims its memory through garbage collection. Shared References and Mutable ObjectsMutable objects can be modified after creation. This requires extra care when using shared references.
Output [0, 2, 3, 4, 5] [0, 2, 3, 4, 5] Explanation:
Creating Independent CopiesIf separate objects are needed, create a copy instead of a shared reference.
Output [0, 2, 3, 4, 5] [1, 2, 3, 4, 5] Explanation:
Comparing Objects Using == and isPython provides two ways to compare objects. Example 1: Shared Reference
Output True True Explanation:
Example 2: Different Objects With Same Values
Output True False Explanation:
Small Integer and String CachingPython internally caches some frequently used immutable objects such as small integers and certain strings to improve performance.
Output True True Explanation:
Assignment vs Copying in Python
Comment Explore | ||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |