{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Copies de variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cela reste simple avec les types simples !" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1\n", "b = a\n", "# On change la valeur de a\n", "a = 2\n", "b" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1\n", "b = a\n", "# On change la valeur de b\n", "b = 2\n", "a" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'toto'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = \"toto\"\n", "b = a\n", "# On change la valeur de a\n", "a = \"tata\"\n", "b" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'toto'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = \"toto\"\n", "b = a\n", "# On change la valeur de b\n", "b = \"tata\"\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## C'est moins simple avec les types... moins simples !\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3]\n", "b = a\n", "a.append(4)\n", "b" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = {1, 2, 3}\n", "b = a\n", "a.add(4)\n", "b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Qu'est-ce qui se passe ?" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on built-in function id in module builtins:\n", "\n", "id(obj, /)\n", " Return the identity of an object.\n", " \n", " This is guaranteed to be unique among simultaneously existing objects.\n", " (CPython uses the object's memory address.)\n", "\n" ] } ], "source": [ "help( id )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Avec les types simples :" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9302176" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1\n", "id(a)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9302176" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(1)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9302176" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a\n", "id(b)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9302208" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 2\n", "id(a)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9302208" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(2)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9302176" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(b)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9302304" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b += 4\n", "id(b)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9302304" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "id(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Avec les types complexes :" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139846274206288" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2]\n", "id(a)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "139846274206288" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a\n", "id(b)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "a.append(3)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n" ] }, { "data": { "text/plain": [ "139846274206288" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(a)\n", "id(a)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n" ] }, { "data": { "text/plain": [ "139846274206288" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(b)\n", "id(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\Rightarrow$ _shallow copy_ par défaut" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pour effectuer des copies profondes :" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = a.copy()\n", "id(a) == id(b)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "b.append(4)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "C'est pareil avec les dictionnaires :" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = {\"key1\": \"value1\", \"key2\": \"value2\"}\n", "b = a\n", "id(a) == id(b)" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = {\"key1\": \"value1\", \"key2\": \"value2\"}\n", "b = a.copy()\n", "id(a) == id(b)" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "b['key3'] = 'value3'" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'key1': 'value1', 'key2': 'value2'}" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] } ], "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.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }