This commit is contained in:
rasbt 2014-04-27 12:06:41 -04:00
parent d87b987f4c
commit a0edd6b293
2 changed files with 132 additions and 28 deletions

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:67969145fdd440ec49d6e6976fa2ea5ffc6db16ed8c1a3f8371040f1c0e97617"
"signature": "sha256:9654ab25e9d989ee8f196ec7cd5c2b7222157dbe195ddf70e6ec34329c9db545"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -13,7 +13,7 @@
"metadata": {},
"source": [
"Sebastian Raschka \n",
"last updated: 04/25/2014\n",
"last updated: 04/27/2014 ([Changelog](#changelog))\n",
"\n",
"[Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/not_so_obvious_python_stuff.ipynb)\n",
"\n",
@ -553,7 +553,7 @@
"metadata": {},
"source": [
"**Shallow copy**: \n",
"If we use the assignment operator to assign one list to another list, we just create a new name reference to the original list. If we want to create a new list object, we have to make a copy of the original list. This can be done via `a_list[:]` of `a_list.copy()`."
"If we use the assignment operator to assign one list to another list, we just create a new name reference to the original list. If we want to create a new list object, we have to make a copy of the original list. This can be done via `a_list[:]` or `a_list.copy()`."
]
},
{
@ -583,17 +583,17 @@
"stream": "stdout",
"text": [
"IDs:\n",
"list1: 4377955288\n",
"list2: 4377955288\n",
"list3: 4377955432\n",
"list4: 4377954784\n",
"list1: 4346366472\n",
"list2: 4346366472\n",
"list3: 4346366408\n",
"list4: 4346366536\n",
"\n",
"list1: [3, 2]\n",
"list1: [3, 2]\n"
]
}
],
"prompt_number": 23
"prompt_number": 1
},
{
"cell_type": "markdown",
@ -1664,9 +1664,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"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 one I am most familiar with), we can't really protect class methods from being used outside the class. \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 one I am most familiar with), we can't really protect class methods from being used outside the class (i.e., by the API user). \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",
"So, when we want to mark a class method as private, we can put a single underscore in front of it. \n",
"If we additionally want to avoid name clashes with other classes that might use the same method names, we can prefix the name with a double-underscore to invoke the name mangling.\n",
"\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:"
@ -1681,14 +1683,14 @@
" print('Hello public world!')\n",
" def __private_method(self):\n",
" print('Hello private world!')\n",
" def __call_private_method_in_class(self):\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._my_class__call_private_method_in_class()"
"my_instance.call_private_method_in_class()"
],
"language": "python",
"metadata": {},
@ -1703,7 +1705,7 @@
]
}
],
"prompt_number": 1
"prompt_number": 11
},
{
"cell_type": "markdown",
@ -2393,7 +2395,7 @@
"source": [
"#### Integer division\n",
"This is a pretty dangerous thing if you are porting code, or executing Python 3 code in Python 2 since the change in integer-division behavior can often go unnoticed. \n",
"So, I still tend to use a `float(3/2)` or `3/2.0` instead of a `3/2` in my Python 3 scripts to save the Python 2 guys some trouble ... (PS: and vice versa, you can `from __future__ import division` in your Python 2 scripts)."
"So, I still tend to use a `float(3)/2` or `3/2.0` instead of a `3/2` in my Python 3 scripts to save the Python 2 guys some trouble ... (PS: and vice versa, you can `from __future__ import division` in your Python 2 scripts)."
]
},
{
@ -2914,6 +2916,13 @@
"#Assigning types to variables as values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -2970,6 +2979,49 @@
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"changelog\"></a>\n",
"<br>\n",
"<br>\n",
"<br>\n",
"<br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Changelog"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 04/27/2014\n",
"- minor fixes of typos\n",
"- single- vs. double-underscore clarification in the private class section."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:67969145fdd440ec49d6e6976fa2ea5ffc6db16ed8c1a3f8371040f1c0e97617"
"signature": "sha256:9654ab25e9d989ee8f196ec7cd5c2b7222157dbe195ddf70e6ec34329c9db545"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -13,7 +13,7 @@
"metadata": {},
"source": [
"Sebastian Raschka \n",
"last updated: 04/25/2014\n",
"last updated: 04/27/2014 ([Changelog](#changelog))\n",
"\n",
"[Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/not_so_obvious_python_stuff.ipynb)\n",
"\n",
@ -553,7 +553,7 @@
"metadata": {},
"source": [
"**Shallow copy**: \n",
"If we use the assignment operator to assign one list to another list, we just create a new name reference to the original list. If we want to create a new list object, we have to make a copy of the original list. This can be done via `a_list[:]` of `a_list.copy()`."
"If we use the assignment operator to assign one list to another list, we just create a new name reference to the original list. If we want to create a new list object, we have to make a copy of the original list. This can be done via `a_list[:]` or `a_list.copy()`."
]
},
{
@ -583,17 +583,17 @@
"stream": "stdout",
"text": [
"IDs:\n",
"list1: 4377955288\n",
"list2: 4377955288\n",
"list3: 4377955432\n",
"list4: 4377954784\n",
"list1: 4346366472\n",
"list2: 4346366472\n",
"list3: 4346366408\n",
"list4: 4346366536\n",
"\n",
"list1: [3, 2]\n",
"list1: [3, 2]\n"
]
}
],
"prompt_number": 23
"prompt_number": 1
},
{
"cell_type": "markdown",
@ -1664,9 +1664,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"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 one I am most familiar with), we can't really protect class methods from being used outside the class. \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 one I am most familiar with), we can't really protect class methods from being used outside the class (i.e., by the API user). \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",
"So, when we want to mark a class method as private, we can put a single underscore in front of it. \n",
"If we additionally want to avoid name clashes with other classes that might use the same method names, we can prefix the name with a double-underscore to invoke the name mangling.\n",
"\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:"
@ -1681,14 +1683,14 @@
" print('Hello public world!')\n",
" def __private_method(self):\n",
" print('Hello private world!')\n",
" def __call_private_method_in_class(self):\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._my_class__call_private_method_in_class()"
"my_instance.call_private_method_in_class()"
],
"language": "python",
"metadata": {},
@ -1703,7 +1705,7 @@
]
}
],
"prompt_number": 1
"prompt_number": 11
},
{
"cell_type": "markdown",
@ -2393,7 +2395,7 @@
"source": [
"#### Integer division\n",
"This is a pretty dangerous thing if you are porting code, or executing Python 3 code in Python 2 since the change in integer-division behavior can often go unnoticed. \n",
"So, I still tend to use a `float(3/2)` or `3/2.0` instead of a `3/2` in my Python 3 scripts to save the Python 2 guys some trouble ... (PS: and vice versa, you can `from __future__ import division` in your Python 2 scripts)."
"So, I still tend to use a `float(3)/2` or `3/2.0` instead of a `3/2` in my Python 3 scripts to save the Python 2 guys some trouble ... (PS: and vice versa, you can `from __future__ import division` in your Python 2 scripts)."
]
},
{
@ -2914,6 +2916,13 @@
"#Assigning types to variables as values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -2970,6 +2979,49 @@
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name=\"changelog\"></a>\n",
"<br>\n",
"<br>\n",
"<br>\n",
"<br>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Changelog"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[[back to top](#sections)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 04/27/2014\n",
"- minor fixes of typos\n",
"- single- vs. double-underscore clarification in the private class section."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,