From 94e6e2e9deeb9679a0057c81cf2a16281abbf029 Mon Sep 17 00:00:00 2001 From: rasbt Date: Wed, 16 Apr 2014 10:34:54 -0400 Subject: [PATCH] class name mangling --- ...t_so_obvious_python_stuff-checkpoint.ipynb | 64 ++++++++++++++++++- not_so_obvious_python_stuff.ipynb | 64 ++++++++++++++++++- 2 files changed, 124 insertions(+), 4 deletions(-) diff --git a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb index 22659f4..6f3c76c 100644 --- a/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb +++ b/.ipynb_checkpoints/not_so_obvious_python_stuff-checkpoint.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:c95db62de6aff7272b782e3cfefff05f4e14396a16ac8bd776eb0e3d886e1010" + "signature": "sha256:60a98b1f79976d4e4878d61f726e1b8a8ec893cc042f8cf213b003feea470384" }, "nbformat": 3, "nbformat_minor": 0, @@ -49,7 +49,8 @@ "- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n", "- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n", "- [When mutable contents of immutable tuples aren't so mutable](#immutable_tuple)\n", - "- [List comprehensions are fast, but generators are faster!?](#list_generator)" + "- [List comprehensions are fast, but generators are faster!?](#list_generator)\n", + "- [Public vs. private class methods and name mangling](#private_class)" ] }, { @@ -1177,6 +1178,65 @@ } ], "prompt_number": 13 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "\n", + "\n", + "## Public vs. private class methods and name mangling\n", + "\n", + "Who has not stumbled across this quote \"we are all consenting adults here\" in the Python community, yet? Unlike in other languages like C++ (sorry, there are many more, but that's I am most familiar with), we can't really protect class methods from being used outside the class. \n", + "All we can do is to indicate methods as private to make clear that they are better not used outside the class, but it is really up to the class user, since \"we are all consenting adults here\"! \n", + "So, when we want to \"make\" class methods private, we just put a double-underscore in front of it (same with other class members), which invokes some name mangling if we want to acess the private class member outside the class! \n", + "This doesn't prevent the class user to access this class member though, but he has to know the trick and also knows that it his own risk...\n", + "\n", + "Let the following example illustrate what I mean:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class my_class():\n", + " def public_method(self):\n", + " print('Hello public world!')\n", + " def __private_method(self):\n", + " print('Hello private world!')\n", + " def call_private_method_in_class(self):\n", + " self.__private_method()\n", + " \n", + "my_instance = my_class()\n", + "\n", + "my_instance.public_method()\n", + "my_instance._my_class__private_method()\n", + "my_instance.call_private_method_in_class()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello public world!\n", + "Hello private world!\n", + "Hello private world!\n" + ] + } + ], + "prompt_number": 28 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {} diff --git a/not_so_obvious_python_stuff.ipynb b/not_so_obvious_python_stuff.ipynb index 22659f4..6f3c76c 100644 --- a/not_so_obvious_python_stuff.ipynb +++ b/not_so_obvious_python_stuff.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:c95db62de6aff7272b782e3cfefff05f4e14396a16ac8bd776eb0e3d886e1010" + "signature": "sha256:60a98b1f79976d4e4878d61f726e1b8a8ec893cc042f8cf213b003feea470384" }, "nbformat": 3, "nbformat_minor": 0, @@ -49,7 +49,8 @@ "- [About lambda and closures-in-a-loop pitfall](#lambda_closure)\n", "- [Python's LEGB scope resolution and the keywords `global` and `nonlocal`](#python_legb)\n", "- [When mutable contents of immutable tuples aren't so mutable](#immutable_tuple)\n", - "- [List comprehensions are fast, but generators are faster!?](#list_generator)" + "- [List comprehensions are fast, but generators are faster!?](#list_generator)\n", + "- [Public vs. private class methods and name mangling](#private_class)" ] }, { @@ -1177,6 +1178,65 @@ } ], "prompt_number": 13 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "\n", + "\n", + "## Public vs. private class methods and name mangling\n", + "\n", + "Who has not stumbled across this quote \"we are all consenting adults here\" in the Python community, yet? Unlike in other languages like C++ (sorry, there are many more, but that's I am most familiar with), we can't really protect class methods from being used outside the class. \n", + "All we can do is to indicate methods as private to make clear that they are better not used outside the class, but it is really up to the class user, since \"we are all consenting adults here\"! \n", + "So, when we want to \"make\" class methods private, we just put a double-underscore in front of it (same with other class members), which invokes some name mangling if we want to acess the private class member outside the class! \n", + "This doesn't prevent the class user to access this class member though, but he has to know the trick and also knows that it his own risk...\n", + "\n", + "Let the following example illustrate what I mean:" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "class my_class():\n", + " def public_method(self):\n", + " print('Hello public world!')\n", + " def __private_method(self):\n", + " print('Hello private world!')\n", + " def call_private_method_in_class(self):\n", + " self.__private_method()\n", + " \n", + "my_instance = my_class()\n", + "\n", + "my_instance.public_method()\n", + "my_instance._my_class__private_method()\n", + "my_instance.call_private_method_in_class()" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Hello public world!\n", + "Hello private world!\n", + "Hello private world!\n" + ] + } + ], + "prompt_number": 28 + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {}