mirror of
https://github.com/rasbt/python_reference.git
synced 2025-01-18 23:37:07 +00:00
finally blocks
This commit is contained in:
parent
db4c1c4586
commit
89b7da504c
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:86af4b61efe1215272820c4e70b826e6b19fe283f8025ff1ef099a6b28d46cc3"
|
||||
"signature": "sha256:90703033799353a31e4e44d4a78b991bbc5f3fceb2709614057dd367c91b2b0f"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -63,7 +63,7 @@
|
|||
"- [`True` and `False` in the datetime module](#datetime_module)\n",
|
||||
"- [Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity](#python_small_int)\n",
|
||||
"- [Shallow vs. deep copies if list contains other structures and objects](#shallow_vs_deep)\n",
|
||||
"- [Picking True values from and and or expressions](#false_true_expressions)\n",
|
||||
"- [Picking `True` values from logical `and`s and `or`s](#false_true_expressions)\n",
|
||||
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
|
||||
"- [Be aware of the consuming generator](#consuming_generator)\n",
|
||||
"- [`bool` is a subclass of `int`](#bool_int)\n",
|
||||
|
@ -577,7 +577,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Picking `True` values from `and` and `or` expressions"
|
||||
"## Picking `True` values from logical `and`s and `or`s"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -591,13 +591,16 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If both values of in a `or` expression are True, Python will select the first one, and the second one in `and` expressions\n",
|
||||
"** Logical `or`: ** \n",
|
||||
"\n",
|
||||
"Or - as a reader suggested - picture it as \n",
|
||||
"`a or b == a if a else b` \n",
|
||||
"`a and b == b if a else a` \n",
|
||||
"- If both values in `or` expressions are `True`, Python will select the first value (e.g., select `\"a\"` in `\"a\" or \"b\"`), and the second one in `and` expressions. \n",
|
||||
"This is also called **short-circuiting** - we already know that the logical `or` must be `True` if the first value is `True` and therefore can omit the evaluation of the second value.\n",
|
||||
"\n",
|
||||
"(Original source: [http://gistroll.com/rolls/21/horizontal_assessments/new](http://gistroll.com/rolls/21/horizontal_assessments/new))"
|
||||
"** Logical `and`: ** \n",
|
||||
"\n",
|
||||
"`a and b == b if a else a` \n",
|
||||
"- If both values in `and` expressions are `True`, Python will select the second value, since for a logical `and`, both values must be true.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -993,7 +996,7 @@
|
|||
"source": [
|
||||
"### `global` vs. `local`\n",
|
||||
"\n",
|
||||
"According to the LEGB rule, Python will first look for a variable in the local scope. So if we set the variable `x = 1` in the `local`ly in the function's scope, it won't have an effect on the `global` `x`."
|
||||
"According to the LEGB rule, Python will first look for a variable in the local scope. So if we set the variable `x = 1` `local`ly in the function's scope, it won't have an effect on the `global` `x`."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -1545,7 +1548,7 @@
|
|||
"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 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. \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",
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:86af4b61efe1215272820c4e70b826e6b19fe283f8025ff1ef099a6b28d46cc3"
|
||||
"signature": "sha256:90703033799353a31e4e44d4a78b991bbc5f3fceb2709614057dd367c91b2b0f"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -63,7 +63,7 @@
|
|||
"- [`True` and `False` in the datetime module](#datetime_module)\n",
|
||||
"- [Python reuses objects for small integers - always use \"==\" for equality, \"is\" for identity](#python_small_int)\n",
|
||||
"- [Shallow vs. deep copies if list contains other structures and objects](#shallow_vs_deep)\n",
|
||||
"- [Picking True values from and and or expressions](#false_true_expressions)\n",
|
||||
"- [Picking `True` values from logical `and`s and `or`s](#false_true_expressions)\n",
|
||||
"- [Don't use mutable objects as default arguments for functions!](#def_mutable_func)\n",
|
||||
"- [Be aware of the consuming generator](#consuming_generator)\n",
|
||||
"- [`bool` is a subclass of `int`](#bool_int)\n",
|
||||
|
@ -577,7 +577,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Picking `True` values from `and` and `or` expressions"
|
||||
"## Picking `True` values from logical `and`s and `or`s"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -591,13 +591,16 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If both values of in a `or` expression are True, Python will select the first one, and the second one in `and` expressions\n",
|
||||
"** Logical `or`: ** \n",
|
||||
"\n",
|
||||
"Or - as a reader suggested - picture it as \n",
|
||||
"`a or b == a if a else b` \n",
|
||||
"`a and b == b if a else a` \n",
|
||||
"- If both values in `or` expressions are `True`, Python will select the first value (e.g., select `\"a\"` in `\"a\" or \"b\"`), and the second one in `and` expressions. \n",
|
||||
"This is also called **short-circuiting** - we already know that the logical `or` must be `True` if the first value is `True` and therefore can omit the evaluation of the second value.\n",
|
||||
"\n",
|
||||
"(Original source: [http://gistroll.com/rolls/21/horizontal_assessments/new](http://gistroll.com/rolls/21/horizontal_assessments/new))"
|
||||
"** Logical `and`: ** \n",
|
||||
"\n",
|
||||
"`a and b == b if a else a` \n",
|
||||
"- If both values in `and` expressions are `True`, Python will select the second value, since for a logical `and`, both values must be true.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -993,7 +996,7 @@
|
|||
"source": [
|
||||
"### `global` vs. `local`\n",
|
||||
"\n",
|
||||
"According to the LEGB rule, Python will first look for a variable in the local scope. So if we set the variable `x = 1` in the `local`ly in the function's scope, it won't have an effect on the `global` `x`."
|
||||
"According to the LEGB rule, Python will first look for a variable in the local scope. So if we set the variable `x = 1` `local`ly in the function's scope, it won't have an effect on the `global` `x`."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -1545,7 +1548,7 @@
|
|||
"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 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. \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",
|
||||
|
|
Loading…
Reference in New Issue
Block a user