mirror of
https://github.com/rasbt/python_reference.git
synced 2025-01-18 23:37:07 +00:00
class name mangling
This commit is contained in:
parent
1cf1d6d56e
commit
94e6e2e9de
|
@ -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": [
|
||||
"<br>\n",
|
||||
"<br>\n",
|
||||
"<a name='private_class'></a>\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": {}
|
||||
|
|
|
@ -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": [
|
||||
"<br>\n",
|
||||
"<br>\n",
|
||||
"<a name='private_class'></a>\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": {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user