mutable objects as default arguments

This commit is contained in:
rasbt 2014-04-15 18:08:23 -04:00
parent 7b07896b8e
commit 2e975a9c30
2 changed files with 8 additions and 4 deletions

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:1b145c7412627e0fa1c08e3b663174712154077c8d8b7740f66d3021a06216a4" "signature": "sha256:86ce70b81c4443d14b2e1a1a75e03c712874e6321abf744f5ecaae9181032e2f"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -200,7 +200,9 @@
"This oddity occurs, because Python tends to stores small integers as the same object, but not so for larger ones! \n", "This oddity occurs, because Python tends to stores small integers as the same object, but not so for larger ones! \n",
"(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)\n", "(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)\n",
"\n", "\n",
"So the take home message is: always use \"==\" for equality, \"is\" for identity!" "So the take home message is: always use \"==\" for equality, \"is\" for identity!\n",
"\n",
"Here is a [nice article](http://python.net/%7Egoodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables) explaining it using \"boxes\" (for people with C background) and \"name tags\" (in the case of Python):"
] ]
}, },
{ {

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:1b145c7412627e0fa1c08e3b663174712154077c8d8b7740f66d3021a06216a4" "signature": "sha256:86ce70b81c4443d14b2e1a1a75e03c712874e6321abf744f5ecaae9181032e2f"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -200,7 +200,9 @@
"This oddity occurs, because Python tends to stores small integers as the same object, but not so for larger ones! \n", "This oddity occurs, because Python tends to stores small integers as the same object, but not so for larger ones! \n",
"(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)\n", "(*I received a comment that this is in fact a CPython artefact and must not necessarily be true in all implementations of Python!*)\n",
"\n", "\n",
"So the take home message is: always use \"==\" for equality, \"is\" for identity!" "So the take home message is: always use \"==\" for equality, \"is\" for identity!\n",
"\n",
"Here is a [nice article](http://python.net/%7Egoodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables) explaining it using \"boxes\" (for people with C background) and \"name tags\" (in the case of Python):"
] ]
}, },
{ {