Last active
June 27, 2018 09:52
-
-
Save travishen/e35da6c501a407fb159abf1fd1cdc11f to your computer and use it in GitHub Desktop.
to understand the idea of mutability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### python variables are memory references" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"10919712\n", | |
"0xa69f20\n", | |
"140043181835248\n", | |
"0x7f5e581b43f0\n" | |
] | |
} | |
], | |
"source": [ | |
"var = 10\n", | |
"print(id(var))\n", | |
"print(hex(id(var)))\n", | |
"string = 'hello world!'\n", | |
"print(id(string))\n", | |
"print(hex(id(string)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### reference counting" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"var = 140616357063792\n", | |
"var_common = 1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"2 <- create an extra reference, affect reference count\n", | |
"3365\n" | |
] | |
} | |
], | |
"source": [ | |
"from sys import getrefcount\n", | |
"print(getrefcount(var), \" <- create an extra reference, affect reference count\") \n", | |
"print(getrefcount(var_common))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1 <- does not affect reference count\n" | |
] | |
} | |
], | |
"source": [ | |
"from ctypes import c_long\n", | |
"print(c_long.from_address(id(var)).value, \" <- does not affect reference count\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### gargabe collection" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 96, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import ctypes\n", | |
"import gc\n", | |
"\n", | |
"def in_gc(object_id):\n", | |
" return object_id in [id(obj) for obj in gc.get_objects()]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 97, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class O:\n", | |
" def refer(self, other):\n", | |
" self.other = other\n", | |
" \n", | |
" @property\n", | |
" def address(self):\n", | |
" return 'self: {0}, other: {1}'.format(hex(id(self)), hex(id(self.other)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 98, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"self: 0x7f5e580f36d8, other: 0x7f5e580f3908\n", | |
"self: 0x7f5e580f3908, other: 0x7f5e580f36d8\n" | |
] | |
} | |
], | |
"source": [ | |
"gc.disable()\n", | |
"# create circular reference\n", | |
"o1 = O()\n", | |
"o2 = O()\n", | |
"o1.other = o2\n", | |
"o2.other = o1\n", | |
"print(o1.address)\n", | |
"print(o2.address)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 99, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"True" | |
] | |
}, | |
"execution_count": 99, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"o1.other.other == o1" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 100, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"1\n", | |
"1\n" | |
] | |
} | |
], | |
"source": [ | |
"o2_object_id = id(o2)\n", | |
"# drop o2\n", | |
"o2 = None\n", | |
"print(c_long.from_address(o2_object_id).value)\n", | |
"\n", | |
"o1_object_id = id(o1)\n", | |
"# drop o1\n", | |
"o1 = None\n", | |
"print(c_long.from_address(o1_object_id).value)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 101, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"True\n", | |
"True\n" | |
] | |
} | |
], | |
"source": [ | |
"print(in_gc(o1_object_id))\n", | |
"print(in_gc(o2_object_id))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 102, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"False\n", | |
"False\n" | |
] | |
} | |
], | |
"source": [ | |
"gc.collect()\n", | |
"print(in_gc(o1_object_id))\n", | |
"print(in_gc(o2_object_id))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### variable re-assignment" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from ctypes import c_long\n", | |
"\n", | |
"a = 10\n", | |
"a_id = id(a)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0xa69f20\n", | |
"165\n" | |
] | |
} | |
], | |
"source": [ | |
"print(hex(a_id))\n", | |
"print(c_long.from_address(a_id).value)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"a = a + 10" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0xa69f20\n", | |
"164\n" | |
] | |
} | |
], | |
"source": [ | |
"# old address\n", | |
"print(hex(a_id))\n", | |
"print(c_long.from_address(a_id).value)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0xa6a060\n" | |
] | |
} | |
], | |
"source": [ | |
"# new address\n", | |
"print(hex(id(a)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### shared reference" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a shared reference with b: True\n" | |
] | |
} | |
], | |
"source": [ | |
"# immutable\n", | |
"a = 10\n", | |
"b = 10\n", | |
"print('a shared reference with b:',hex(id(a)) == hex(id(b)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a shared reference with b: False\n" | |
] | |
} | |
], | |
"source": [ | |
"b = 20\n", | |
"print('a shared reference with b:',hex(id(a)) == hex(id(b)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"l1 shared reference with l2: False\n", | |
"l1 shared reference with l3: True\n" | |
] | |
} | |
], | |
"source": [ | |
"# mutable\n", | |
"l1 = [1, 2, 3]\n", | |
"l2 = [1, 2, 3]\n", | |
"l3 = l1\n", | |
"print('l1 shared reference with l2:', hex(id(l1)) == hex(id(l2)))\n", | |
"print('l1 shared reference with l3:', hex(id(l1)) == hex(id(l3)))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"l1 shared reference with l3: True\n" | |
] | |
} | |
], | |
"source": [ | |
"l3.append(4)\n", | |
"print('l1 shared reference with l3:', hex(id(l1)) == hex(id(l3)))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment