|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "24d1a5a7", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "### We can find the memory address that a variable references, by using the id() function.\n", |
| 9 | + "\n", |
| 10 | + "#### The id() function returns the memory address of its argument as a base-10 integer.\n", |
| 11 | + "\n", |
| 12 | + "##### We can use the function hex() to convert the base-10 number to base-16." |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": 1, |
| 18 | + "id": "16a1c962", |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [ |
| 21 | + { |
| 22 | + "name": "stdout", |
| 23 | + "output_type": "stream", |
| 24 | + "text": [ |
| 25 | + "my_var = 10\n", |
| 26 | + "memory address of my_var (decimal): 1736376674896\n", |
| 27 | + "memory address of my_var (hex): 0x194481d6a50\n" |
| 28 | + ] |
| 29 | + } |
| 30 | + ], |
| 31 | + "source": [ |
| 32 | + "my_var = 10\n", |
| 33 | + "print('my_var = {0}'.format(my_var))\n", |
| 34 | + "print('memory address of my_var (decimal): {0}'.format(id(my_var)))\n", |
| 35 | + "print('memory address of my_var (hex): {0}'.format(hex(id(my_var))))" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 2, |
| 41 | + "id": "06a11d30", |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [ |
| 44 | + { |
| 45 | + "name": "stdout", |
| 46 | + "output_type": "stream", |
| 47 | + "text": [ |
| 48 | + "greeting = Hello\n", |
| 49 | + "memory address of my_var (decimal): 1736464331376\n", |
| 50 | + "memory address of my_var (hex): 0x1944d56f270\n" |
| 51 | + ] |
| 52 | + } |
| 53 | + ], |
| 54 | + "source": [ |
| 55 | + "greeting = 'Hello'\n", |
| 56 | + "print('greeting = {0}'.format(greeting))\n", |
| 57 | + "print('memory address of my_var (decimal): {0}'.format(id(greeting)))\n", |
| 58 | + "print('memory address of my_var (hex): {0}'.format(hex(id(greeting))))" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "markdown", |
| 63 | + "id": "d671e16a", |
| 64 | + "metadata": {}, |
| 65 | + "source": [ |
| 66 | + "ote how the memory address of my_var is different from that of greeting.\n", |
| 67 | + "\n", |
| 68 | + "Strictly speaking, my_var is not \"equal\" to 10.\n", |
| 69 | + "\n", |
| 70 | + "Instead my_var is a reference to an (integer) object (containing the value 10) located at the memory address id(my_var)\n", |
| 71 | + "\n", |
| 72 | + "Similarly for the variable greeting." |
| 73 | + ] |
| 74 | + } |
| 75 | + ], |
| 76 | + "metadata": { |
| 77 | + "kernelspec": { |
| 78 | + "display_name": "Python 3 (ipykernel)", |
| 79 | + "language": "python", |
| 80 | + "name": "python3" |
| 81 | + }, |
| 82 | + "language_info": { |
| 83 | + "codemirror_mode": { |
| 84 | + "name": "ipython", |
| 85 | + "version": 3 |
| 86 | + }, |
| 87 | + "file_extension": ".py", |
| 88 | + "mimetype": "text/x-python", |
| 89 | + "name": "python", |
| 90 | + "nbconvert_exporter": "python", |
| 91 | + "pygments_lexer": "ipython3", |
| 92 | + "version": "3.9.12" |
| 93 | + } |
| 94 | + }, |
| 95 | + "nbformat": 4, |
| 96 | + "nbformat_minor": 5 |
| 97 | +} |
0 commit comments