Understand Python’s memory model — mutable vs immutable types, object identity, integer caching, reference counting, and garbage collection.
Writing efficient and bug-free Python code becomes much easier once you understand what happens under the hood. Every value you create lives as an object in memory, variables are pointers to those objects rather than containers for raw data, and Python manages memory automatically through reference counting and garbage collection. This page demystifies Python’s memory model so you can reason confidently about object identity, mutable vs. immutable types, and why is and == sometimes give surprising results.
Mutability vs. Immutability
Every variable in Python is a reference pointing to an object in memory. Objects fall into two categories:
Mutable Objects
A mutable object can be changed after it is created. Its memory address stays the same even after modification:
numbers = [1, 2, 3]print(id(numbers)) # e.g. 4398114048numbers.append(4)print(numbers) # [1, 2, 3, 4]print(id(numbers)) # 4398114048 — same address!
Common mutable types: list, dict, set
Immutable Objects
An immutable object cannot be changed. Any “modification” creates a new object at a new memory address:
name = "Alice"print(id(name)) # e.g. 4398201904name += " Smith"print(id(name)) # e.g. 4398205424 — different address!
Common immutable types: int, float, str, tuple, bool
Tuples are immutable, but if a tuple contains a mutable object like a list, the list’s contents can still be modified:
my_tuple = (1, [2, 3])my_tuple[1].append(4)print(my_tuple) # (1, [2, 3, 4]) — the list changed!
The tuple itself didn’t change (it still references the same list), but the list’s contents did.
Everything is an Object
In Python, every value is a first-class object, including integers, strings, functions, modules, and classes. Each object carries three things:
A Value — the data itself.
A Type — defines what the object can do.
An Identity — a unique integer from id() representing the memory address.
is (identity): compares memory addresses — checks if two variables point to the exact same object.
list_a = [1, 2, 3]list_b = [1, 2, 3]print(list_a == list_b) # True — same valueprint(list_a is list_b) # False — different objects in memoryprint(id(list_a) == id(list_b)) # False
Integer Caching: CPython caches integers from -5 to 256 for performance. This means small integers with the same value share the same object:
x = 100y = 100print(x is y) # True — cached, same objecta = 300b = 300print(a is b) # False — not cached, different objects
Never rely on is for integer comparisons. Use == instead.
Reference Counting
Python manages memory automatically. Its primary mechanism is reference counting:
Every object tracks how many variables reference it.
When you assign an object to a variable, its reference count increases by 1.
When a variable goes out of scope or is reassigned, the count decreases by 1.
When the count reaches 0, Python destroys the object and frees its memory.
import sysa = [1, 2, 3] # ref count = 1b = a # ref count = 2# sys.getrefcount() temporarily adds 1 during the callprint(sys.getrefcount(a)) # 3
Circular references — where Object A references Object B and Object B references Object A — can prevent reference counts from ever reaching zero. Python detects and cleans these up using a secondary Generational Garbage Collector. You can trigger it manually with gc.collect() from the gc module.
Summary
Concept
Key Point
Mutable types
Changed in-place; id() stays the same
Immutable types
Modification creates a new object at a new address
==
Compares values
is
Compares memory addresses (object identity)
Integer caching
-5 to 256 reuse the same object in CPython
Reference counting
Object is freed when its reference count hits 0
Garbage collector
Handles circular references that defeat reference counting
{"docsConfig":{"theme":"palm","$schema":"https://mintlify.com/docs.json","name":"Python for AI \u0026 FastAPI","colors":{"primary":"#16A34A","light":"#07C983","dark":"#15803D"},"favicon":"https://mintcdn.com/fastapi2day/o2Jso_KhqkR4_ilm/favicon.svg?fit=max\u0026auto=format\u0026n=o2Jso_KhqkR4_ilm\u0026q=85\u0026s=d4ea6ac51fe137dae217309616455228","navbar":{"primary":{"type":"button","label":"Start Learning","href":"https://sgrinfo.tech"}},"footer":{},"styling":{"latex":false},"navigation":{"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"fastapi2day","hasAudioNarration":false,"actualSubdomain":"fastapi2day","customDomains":["fastapi2day.codewithsiva.dev"],"gitSource":{"type":"github","owner":"mintlify-community","repo":"docs-sgrinfotech-31e4544d","deployBranch":"main","contentDirectory":"","isPrivate":true,"mountPath":"","label":""},"entitlements":{},"buildId":"6aea217d8509c0a36927aa68:success","clientVersion":"0.0.4347","searchFilterCounts":[{"language":"default","tag":"Data \u0026 APIs","count":31},{"language":"default","tag":"Python","count":24},{"language":"default","tag":"Getting Started","count":21},{"language":"default","tag":"Projects \u0026 Tools","count":19},{"language":"default","tag":"FastAPI","count":15},{"language":"default","tag":"Advanced","count":12},{"language":"default","tag":"Basics","count":7},{"language":"default","tag":"Environments \u0026 Packages","count":7},{"language":"default","tag":"VS Code","count":6},{"language":"default","tag":"Python Setup","count":6}]},"assistantConfig":{"enableAskAiButton":false,"enableStarterQuestions":false,"enableContextualStarterQuestions":false,"enableAskAiSearchBar":true},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[{"name":"kill-isr","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-rbac-roles","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-mintlify-slack","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-integrations-settings","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"rbac-expansion","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-pr-grouping","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-prefix","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"analytics-v7","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"user-flows-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"no-publish-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving-live","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"stripTemperatureMiddleware","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"support-platform-pylon-zendesk","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"instant-rollback","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications-server","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-fable","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fixed-ai-action-pricing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"site-roles","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automation-resolve-merge-conflict","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"scoped-publishing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"mintql","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"page-level-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-version-history-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-page-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"full-page-search","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workspace-projects","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fork-pr-previews","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"revalidation-jwt","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"hobby-abuse-scan","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"dashboard-inbox","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-dual-write","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"approve-review-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-metadata","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-serve","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"live-agents","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"starter-pricing","enabled":true,"variant":{"name":"treatment","enabled":true,"payload":{"type":"string","value":"treatment"},"feature_enabled":true},"impressionData":false},{"name":"llms-txt-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"wildcard-email-auth","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false}],"starterQuestions":[],"googleFontsInlined":false},"customCss":[]}
{"page":{"pagePath":"advanced-python/internals","slug":"/advanced-python/internals","pageMetadata":{"title":"Python Internals: Memory Model, Objects \u0026 References","sidebarTitle":"Python Internals","description":"Understand Python's memory model — mutable vs immutable types, object identity, integer caching, reference counting, and garbage collection.","href":"/advanced-python/internals"},"description":{"compiledSource":"\"use strict\";\nconst {jsx: _jsx} = arguments[0];\nconst {useMDXComponents: _provideComponents} = arguments[0];\nfunction _createMdxContent(props) {\n const _components = {\n p: \"p\",\n ..._provideComponents(),\n ...props.components\n };\n return _jsx(_components.p, {\n children: \"Understand Python’s memory model — mutable vs immutable types, object identity, integer caching, reference counting, and garbage collection.\"\n });\n}\nfunction MDXContent(props = {}) {\n const {wrapper: MDXLayout} = {\n ..._provideComponents(),\n ...props.components\n };\n return MDXLayout ? _jsx(MDXLayout, {\n ...props,\n children: _jsx(_createMdxContent, {\n ...props\n })\n }) : _createMdxContent(props);\n}\nreturn {\n default: MDXContent\n};\n","frontmatter":{},"scope":{}},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={code:`code`,li:`li`,ol:`ol`,p:`p`,pre:`pre`,span:`span`,strong:`strong`,tbody:`tbody`,td:`td`,th:`th`,thead:`thead`,tr:`tr`,ul:`ul`,..._provideComponents(),...props.components},{CodeBlock,Heading,Table,Tip,Warning}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Table)_missingMdxReference(`Table`,true);if(!Tip)_missingMdxReference(`Tip`,true);if(!Warning)_missingMdxReference(`Warning`,true);return _jsxs(_Fragment,{children:[_jsxs(_components.p,{children:[`Writing efficient and bug-free Python code becomes much easier once you understand what happens under the hood. Every value you create lives as an object in memory, variables are pointers to those objects rather than containers for raw data, and Python manages memory automatically through reference counting and garbage collection. This page demystifies Python’s memory model so you can reason confidently about object identity, mutable vs. immutable types, and why `,_jsx(_components.code,{children:`is`}),` and `,_jsx(_components.code,{children:`==`}),` sometimes give surprising results.`]}),`\n`,_jsx(Heading,{level:`2`,id:`mutability-vs-immutability`,children:`Mutability vs. Immutability`}),`\n`,_jsxs(_components.p,{children:[`Every variable in Python is a `,_jsx(_components.strong,{children:`reference`}),` pointing to an object in memory. Objects fall into two categories:`]}),`\n`,_jsx(Heading,{level:`3`,id:`mutable-objects`,children:`Mutable Objects`}),`\n`,_jsxs(_components.p,{children:[`A `,_jsx(_components.strong,{children:`mutable`}),` object can be changed after it is created. Its memory address stays the same even after modification:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`6`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`6`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`numbers `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` [`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`3`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`]`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`id`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(numbers)) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# e.g. 4398114048`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`numbers.append(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`4`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(numbers) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# [1, 2, 3, 4]`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`id`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(numbers)) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# 4398114048 — same address!`})]}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`Common mutable types: `,_jsx(_components.strong,{children:_jsx(_components.code,{children:`list`})}),`, `,_jsx(_components.strong,{children:_jsx(_components.code,{children:`dict`})}),`, `,_jsx(_components.strong,{children:_jsx(_components.code,{children:`set`})})]}),`\n`,_jsx(Heading,{level:`3`,id:`immutable-objects`,children:`Immutable Objects`}),`\n`,_jsxs(_components.p,{children:[`An `,_jsx(_components.strong,{children:`immutable`}),` object cannot be changed. Any “modification” creates a `,_jsx(_components.strong,{children:`new`}),` object at a new memory address:`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`5`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`5`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`name `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"Alice\"`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`id`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(name)) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# e.g. 4398201904`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`name `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`+=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \" Smith\"`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`id`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(name)) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# e.g. 4398205424 — different address!`})]}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[`Common immutable types: `,_jsx(_components.strong,{children:_jsx(_components.code,{children:`int`})}),`, `,_jsx(_components.strong,{children:_jsx(_components.code,{children:`float`})}),`, `,_jsx(_components.strong,{children:_jsx(_components.code,{children:`str`})}),`, `,_jsx(_components.strong,{children:_jsx(_components.code,{children:`tuple`})}),`, `,_jsx(_components.strong,{children:_jsx(_components.code,{children:`bool`})})]}),`\n`,_jsxs(Warning,{children:[_jsxs(_components.p,{children:[`Tuples are immutable, but if a tuple `,_jsx(_components.strong,{children:`contains`}),` a mutable object like a list, the list’s contents can still be modified:`]}),_jsx(CodeBlock,{filename:``,numberOfLines:`3`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`3`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`my_tuple `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` (`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, [`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`3`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`])`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`my_tuple[`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`].append(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`4`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(my_tuple) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# (1, [2, 3, 4]) — the list changed!`})]}),`\n`]})})}),_jsx(_components.p,{children:`The tuple itself didn’t change (it still references the same list), but the list’s contents did.`})]}),`\n`,_jsx(Heading,{level:`2`,id:`everything-is-an-object`,children:`Everything is an Object`}),`\n`,_jsxs(_components.p,{children:[`In Python, `,_jsx(_components.strong,{children:`every value is a first-class object`}),`, including integers, strings, functions, modules, and classes. Each object carries three things:`]}),`\n`,_jsxs(_components.ol,{children:[`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`A Value`}),` — the data itself.`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`A Type`}),` — defines what the object can do.`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:`An Identity`}),` — a unique integer from `,_jsx(_components.code,{children:`id()`}),` representing the memory address.`]}),`\n`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`7`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`7`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`def`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` greet`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`():`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` return`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"Hello!\"`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Functions are objects too`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`func_reference `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` greet`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(func_reference()) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# Hello!`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#4EC9B0`},children:`type`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(greet)) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# \u003cclass 'function'>`})]}),`\n`]})})}),`\n`,_jsxs(Heading,{level:`2`,id:`identity-id-vs-equality-==`,children:[`Identity (`,_jsx(_components.code,{children:`id()`}),`) vs. Equality (`,_jsx(_components.code,{children:`==`}),`)`]}),`\n`,_jsx(_components.p,{children:`This distinction is one of the most important in Python:`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:_jsx(_components.code,{children:`==`})}),` (equality): compares `,_jsx(_components.strong,{children:`values`}),` — calls `,_jsx(_components.code,{children:`__eq__`}),` internally.`]}),`\n`,_jsxs(_components.li,{children:[_jsx(_components.strong,{children:_jsx(_components.code,{children:`is`})}),` (identity): compares `,_jsx(_components.strong,{children:`memory addresses`}),` — checks if two variables point to the exact same object.`]}),`\n`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`6`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`6`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`list_a `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` [`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`3`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`]`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`list_b `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` [`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`3`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`]`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(list_a `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`==`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` list_b) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# True — same value`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(list_a `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`is`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` list_b) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# False — different objects in memory`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`id`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(list_a) `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`==`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:` id`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(list_b)) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# False`})]}),`\n`]})})}),`\n`,_jsxs(Tip,{children:[_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Integer Caching:`}),` CPython caches integers from `,_jsx(_components.code,{children:`-5`}),` to `,_jsx(_components.code,{children:`256`}),` for performance. This means small integers with the same value share the same object:`]}),_jsx(CodeBlock,{filename:``,numberOfLines:`7`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`7`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`x `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 100`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`y `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 100`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(x `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`is`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` y) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# True — cached, same object`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`a `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 300`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`b `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:` 300`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(a `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`is`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` b) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# False — not cached, different objects`})]}),`\n`]})})}),_jsxs(_components.p,{children:[`Never rely on `,_jsx(_components.code,{children:`is`}),` for integer comparisons. Use `,_jsx(_components.code,{children:`==`}),` instead.`]})]}),`\n`,_jsx(Heading,{level:`2`,id:`reference-counting`,children:`Reference Counting`}),`\n`,_jsxs(_components.p,{children:[`Python manages memory automatically. Its primary mechanism is `,_jsx(_components.strong,{children:`reference counting`}),`:`]}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:`Every object tracks how many variables reference it.`}),`\n`,_jsx(_components.li,{children:`When you assign an object to a variable, its reference count increases by 1.`}),`\n`,_jsx(_components.li,{children:`When a variable goes out of scope or is reassigned, the count decreases by 1.`}),`\n`,_jsxs(_components.li,{children:[`When the count reaches `,_jsx(_components.strong,{children:`0`}),`, Python destroys the object and frees its memory.`]}),`\n`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`7`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`7`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:`import`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` sys`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`a `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` [`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`1`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`2`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`3`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`] `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# ref count = 1`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`b `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` a `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# ref count = 2`})]}),`\n`,_jsx(_components.span,{className:`line`}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# sys.getrefcount() temporarily adds 1 during the call`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#DCDCAA`},children:`print`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(sys.getrefcount(a)) `}),_jsx(_components.span,{style:{color:`#6E7781`,\"--shiki-dark\":`#6A9955`},children:`# 3`})]}),`\n`]})})}),`\n`,_jsx(Warning,{children:_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Circular references`}),` — where Object A references Object B and Object B references Object A — can prevent reference counts from ever reaching zero. Python detects and cleans these up using a secondary `,_jsx(_components.strong,{children:`Generational Garbage Collector`}),`. You can trigger it manually with `,_jsx(_components.code,{children:`gc.collect()`}),` from the `,_jsx(_components.code,{children:`gc`}),` module.`]})}),`\n`,_jsx(Heading,{level:`2`,id:`summary`,children:`Summary`}),`\n`,_jsxs(Table,{children:[_jsx(_components.thead,{children:_jsxs(_components.tr,{children:[_jsx(_components.th,{children:`Concept`}),_jsx(_components.th,{children:`Key Point`})]})}),_jsxs(_components.tbody,{children:[_jsxs(_components.tr,{children:[_jsx(_components.td,{children:`Mutable types`}),_jsxs(_components.td,{children:[`Changed in-place; `,_jsx(_components.code,{children:`id()`}),` stays the same`]})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:`Immutable types`}),_jsx(_components.td,{children:`Modification creates a new object at a new address`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.code,{children:`==`})}),_jsx(_components.td,{children:`Compares values`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:_jsx(_components.code,{children:`is`})}),_jsx(_components.td,{children:`Compares memory addresses (object identity)`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:`Integer caching`}),_jsxs(_components.td,{children:[_jsx(_components.code,{children:`-5`}),` to `,_jsx(_components.code,{children:`256`}),` reuse the same object in CPython`]})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:`Reference counting`}),_jsx(_components.td,{children:`Object is freed when its reference count hits 0`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{children:`Garbage collector`}),_jsx(_components.td,{children:`Handles circular references that defeat reference counting`})]})]})]})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"Python Internals: Memory Model, Objects \u0026 References","sidebarTitle":"Python Internals","description":"Understand Python's memory model — mutable vs immutable types, object identity, integer caching, reference counting, and garbage collection.","href":"/advanced-python/internals"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"Mutability vs. Immutability","slug":"mutability-vs-immutability","depth":2,"children":[{"title":"Mutable Objects","slug":"mutable-objects","depth":3,"children":[]},{"title":"Immutable Objects","slug":"immutable-objects","depth":3,"children":[]}]},{"title":"Everything is an Object","slug":"everything-is-an-object","depth":2,"children":[]},{"title":"Identity (id()) vs. Equality (==)","slug":"identity-id-vs-equality-==","depth":2,"children":[]},{"title":"Reference Counting","slug":"reference-counting","depth":2,"children":[]},{"title":"Summary","slug":"summary","depth":2,"children":[]}],"codeExamples":{}},"jsLinks":[],"subdomain":"fastapi2day","siteName":"Python for AI \u0026 FastAPI","title":"Python Internals: Memory Model, Objects \u0026 References - Python for AI \u0026 FastAPI","contextualStarterQuestions":[],"navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2026-08-03T17:36:36.554Z","originalFileLocation":"advanced-python/internals.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"system\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"description\" content=\"Understand Python\u0026#x27;s memory model — mutable vs immutable types, object identity, integer caching, reference counting, and garbage collection.\">\u003cmeta data-mint-head name=\"application-name\" content=\"Python for AI \u0026amp; FastAPI\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/svg+xml\" sizes=\"180x180\" href=\"https://mintcdn.com/fastapi2day/o2Jso_KhqkR4_ilm/favicon.svg?fit=max\u0026amp;auto=format\u0026amp;n=o2Jso_KhqkR4_ilm\u0026amp;q=85\u0026amp;s=d4ea6ac51fe137dae217309616455228\">\u003clink data-mint-head rel=\"icon\" type=\"image/svg+xml\" sizes=\"32x32\" href=\"https://mintcdn.com/fastapi2day/o2Jso_KhqkR4_ilm/favicon.svg?fit=max\u0026amp;auto=format\u0026amp;n=o2Jso_KhqkR4_ilm\u0026amp;q=85\u0026amp;s=d4ea6ac51fe137dae217309616455228\">\u003clink data-mint-head rel=\"icon\" type=\"image/svg+xml\" sizes=\"16x16\" href=\"https://mintcdn.com/fastapi2day/o2Jso_KhqkR4_ilm/favicon.svg?fit=max\u0026amp;auto=format\u0026amp;n=o2Jso_KhqkR4_ilm\u0026amp;q=85\u0026amp;s=d4ea6ac51fe137dae217309616455228\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/svg+xml\" href=\"https://mintcdn.com/fastapi2day/o2Jso_KhqkR4_ilm/favicon.svg?fit=max\u0026amp;auto=format\u0026amp;n=o2Jso_KhqkR4_ilm\u0026amp;q=85\u0026amp;s=d4ea6ac51fe137dae217309616455228\">\u003cmeta data-mint-head property=\"og:title\" content=\"Python Internals: Memory Model, Objects \u0026amp; References - Python for AI \u0026amp; FastAPI\">\u003cmeta data-mint-head property=\"og:description\" content=\"Understand Python\u0026#x27;s memory model — mutable vs immutable types, object identity, integer caching, reference counting, and garbage collection.\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"Python for AI \u0026amp; FastAPI\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://fastapi2day.codewithsiva.dev/advanced-python/internals\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://fastapi2day.mintlify.site/_mintlify/api/og/v2?division=Advanced\u0026amp;title=Python+Internals%3A+Memory+Model%2C+Objects+%26+References\u0026amp;description=Understand+Python%27s+memory+model+%E2%80%94+mutable+vs+immutable+types%2C+object+identity%2C+integer+caching%2C+reference+counting%2C+and+garbage+collection.\u0026amp;theme=be394bdd824f66fd2549706a\u0026amp;signature=1edb225fe48390d5d843101e28649023dcc1591148f645acdb6effa78f48f9b3\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"Python Internals: Memory Model, Objects \u0026amp; References - Python for AI \u0026amp; FastAPI\">\u003cmeta data-mint-head name=\"twitter:description\" content=\"Understand Python\u0026#x27;s memory model — mutable vs immutable types, object identity, integer caching, reference counting, and garbage collection.\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://fastapi2day.mintlify.site/_mintlify/api/og/v2?division=Advanced\u0026amp;title=Python+Internals%3A+Memory+Model%2C+Objects+%26+References\u0026amp;description=Understand+Python%27s+memory+model+%E2%80%94+mutable+vs+immutable+types%2C+object+identity%2C+integer+caching%2C+reference+counting%2C+and+garbage+collection.\u0026amp;theme=be394bdd824f66fd2549706a\u0026amp;signature=1edb225fe48390d5d843101e28649023dcc1591148f645acdb6effa78f48f9b3\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"https://mintlify.s3.us-west-1.amazonaws.com/fastapi2day/_generated/favicon/browserconfig.xml?v=3\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"Python for AI \u0026amp; FastAPI\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#16A34A\">\u003clink data-mint-head rel=\"canonical\" href=\"https://fastapi2day.codewithsiva.dev/advanced-python/internals\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/advanced-python/internals.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://fastapi2day.codewithsiva.dev/#organization\",\"name\":\"Python for AI \u0026 FastAPI\",\"url\":\"https://fastapi2day.codewithsiva.dev\"},{\"@type\":\"WebSite\",\"@id\":\"https://fastapi2day.codewithsiva.dev#website\",\"name\":\"Python for AI \u0026 FastAPI\",\"url\":\"https://fastapi2day.codewithsiva.dev\",\"publisher\":{\"@id\":\"https://fastapi2day.codewithsiva.dev/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://fastapi2day.codewithsiva.dev/advanced-python/internals#webpage\",\"url\":\"https://fastapi2day.codewithsiva.dev/advanced-python/internals\",\"name\":\"Python Internals: Memory Model, Objects \u0026 References\",\"description\":\"Understand Python's memory model — mutable vs immutable types, object identity, integer caching, reference counting, and garbage collection.\",\"dateModified\":\"2026-08-03T17:36:36.554Z\",\"isPartOf\":{\"@id\":\"https://fastapi2day.codewithsiva.dev#website\"},\"breadcrumb\":{\"@id\":\"https://fastapi2day.codewithsiva.dev/advanced-python/internals#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://fastapi2day.codewithsiva.dev/advanced-python/internals#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Python for AI \u0026 FastAPI\",\"item\":\"https://fastapi2day.codewithsiva.dev\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Internals: Memory Model, Objects \u0026 References\",\"item\":\"https://fastapi2day.codewithsiva.dev/advanced-python/internals\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://fastapi2day.codewithsiva.dev/advanced-python/internals#article\",\"headline\":\"Python Internals: Memory Model, Objects \u0026 References\",\"name\":\"Python Internals: Memory Model, Objects \u0026 References\",\"description\":\"Understand Python's memory model — mutable vs immutable types, object identity, integer caching, reference counting, and garbage collection.\",\"url\":\"https://fastapi2day.codewithsiva.dev/advanced-python/internals\",\"mainEntityOfPage\":{\"@id\":\"https://fastapi2day.codewithsiva.dev/advanced-python/internals#webpage\"},\"dateModified\":\"2026-08-03T17:36:36.554Z\",\"publisher\":{\"@id\":\"https://fastapi2day.codewithsiva.dev/#organization\"},\"isPartOf\":{\"@id\":\"https://fastapi2day.codewithsiva.dev#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"Python for AI \u0026 FastAPI\",\"publisher\":{\"@type\":\"Organization\",\"name\":\"Python for AI \u0026 FastAPI\"},\"provider\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","agentPointerHref":"/advanced-python/internals.md","siteName":"Python for AI \u0026 FastAPI","nav":{"global":null,"tabs":[{"tab":"Introduction","pages":[{"title":"Introduction","href":""}]},{"tab":"Getting Started","pages":[{"title":"Getting Started","href":"/getting-started"}]},{"tab":"Python","groups":[{"group":"Basics","pages":[{"title":"Python Basics: Syntax, Variables, Types \u0026 Control Flow","sidebarTitle":"Python Basics","href":"/basic-python/python-basics"},{"title":"Python Data Structures: Lists, Tuples, Dicts \u0026 Sets","sidebarTitle":"Data Structures","href":"/basic-python/data-structures"},{"title":"Packing \u0026 Unpacking in Python: Starred Expressions","sidebarTitle":"Packing \u0026 Unpacking","href":"/basic-python/packing-unpacking"},{"title":"Python Functions: Parameters, Scope, Closures \u0026 Decorators","sidebarTitle":"Functions","href":"/basic-python/functions"},{"title":"Python Modules, Packages \u0026 the Standard Library Guide","sidebarTitle":"Modules","href":"/basic-python/modules-packages"},{"title":"Python OOP \u0026 Classes: Blueprints, Inheritance \u0026 More","sidebarTitle":"OOP \u0026 Classes","href":"/basic-python/oop-classes"},{"title":"Python Exception Handling: try, except \u0026 Custom Errors","sidebarTitle":"Exceptions","description":"Handle runtime errors gracefully with try-except, raise custom exceptions, and use else and finally for clean resource management.","href":"/basic-python/exception-handling"}]},{"group":"Advanced","pages":[{"title":"Python Internals: Memory Model, Objects \u0026 References","sidebarTitle":"Python Internals","href":"/advanced-python/internals"},{"title":"Python Comprehensions, Iterators \u0026 Generators Guide","sidebarTitle":"Comprehensions","description":"Write expressive Pythonic code using list, dict, and set comprehensions alongside custom iterators, memory-efficient generators, and context managers.","href":"/advanced-python/comprehensions"},{"title":"Advanced Functions: Lambdas, Closures \u0026 Decorators","sidebarTitle":"Advanced Functions","href":"/advanced-python/functions"},{"title":"Functional Programming in Python: map, filter \u0026 reduce","sidebarTitle":"Functional","href":"/advanced-python/functional-programming"},{"title":"Advanced OOP in Python: Metaclasses, Dunders \u0026 Frameworks","sidebarTitle":"Advanced OOP","href":"/advanced-python/advanced-oop"},{"title":"Context Managers: Setup, Cleanup \u0026 the with Statement","sidebarTitle":"Context Managers","href":"/advanced-python/context-mgmt"},{"title":"Working with Data Files: Text, JSON \u0026 CSV in Python","sidebarTitle":"Working with Data","href":"/advanced-python/working-with-data"},{"title":"Python .env Files: Managing Secrets and Config Safely","sidebarTitle":"dotenv","href":"/advanced-python/dotenv"},{"title":"Async Programming in Python: async, await \u0026 asyncio","sidebarTitle":"Async","href":"/advanced-python/async-programming"},{"title":"Dependency Injection in Python: Decoupling Your Code","sidebarTitle":"Dependency Injection","href":"/advanced-python/di"},{"title":"Python Type Hints: Annotate Variables and Functions","sidebarTitle":"Type Hints","href":"/advanced-python/type-hints"},{"title":"Python Dataclasses: Clean Data Models Without Boilerplate","sidebarTitle":"Dataclasses","href":"/advanced-python/data-class"}]},{"group":"Pydantic","pages":[{"title":"Pydantic Introduction: Runtime Data Validation in Python","sidebarTitle":"Introduction","href":"/pydantic/introduction"},{"title":"Your First Pydantic Model: A BaseModel Basics Guide","sidebarTitle":"First Model","href":"/pydantic/your-first-model"},{"title":"Pydantic Validation and Fields: A Constraints Guide","sidebarTitle":"Validation","href":"/pydantic/validation-and-fields"},{"title":"Pydantic Nested Models: Composing Complex Data Structures","sidebarTitle":"Nested Models","href":"/pydantic/nested-models"},{"title":"Pydantic Settings: Type-Safe Application Configuration","sidebarTitle":"Settings","href":"/pydantic/pydantic-settings"}]}]},{"tab":"Projects \u0026 Tools","pages":[{"title":"Projects \u0026 Tools","href":"/tools"}]},{"tab":"Data \u0026 APIs","pages":[{"title":"Data \u0026 APIs","href":"/sql/intro"}]}]},"basePath":"","title":"Python Internals: Memory Model, Objects \u0026 References - Python for AI \u0026 FastAPI","deploymentId":"dpl_6UE2cqB47X6P7P3F2hD3UphW6ijc","buildId":"dpl_6UE2cqB47X6P7P3F2hD3UphW6ijc","dynamics":[],"chunks":[]}