Skip to content

Instantly share code, notes, and snippets.

@manojpandey
Last active August 11, 2017 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manojpandey/911152c53b2f99ecd1950167c5383eaa to your computer and use it in GitHub Desktop.
Save manojpandey/911152c53b2f99ecd1950167c5383eaa to your computer and use it in GitHub Desktop.
PyCon Korea 2017 - Manoj Pandey (Attack of Pythons: Gotchas and Landmines in Python)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"<img src=\"https://s3.amazonaws.com/manojp-personal/intro.png\">"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"<img src=\"https://s3.amazonaws.com/manojp-personal/about.png\">"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"// * Python 2"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# List repetition with nested lists\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"outputs": [],
"source": [
"my_list = [[0]*5]*4\n",
"for i in range(3):\n",
" my_list[2][i] = 1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0], [1, 1, 1, 0, 0]]\n"
]
}
],
"source": [
"print my_list"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"new_list = [[0]*5 for x in range(4)]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [1, 1, 1, 0, 0], [0, 0, 0, 0, 0]]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"for i in range(3):\n",
" new_list[2][i] = 1\n",
"new_list"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Don't mix spaces and tabs\n",
"\n",
"> Just don't. You would cry.\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"where, Tab(→) = 4 spaces\n",
"\n",
" → Some code here(firstArgument,\n",
" → → → → ···secondOne,\n",
" → → → ·······modified);"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"where, Tab(→)= 2 spaces\n",
"\n",
" → Some code here(firstArgument,\n",
" → → → → ···secondOne,\n",
" → → → ·······modified);"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Explicit type cast of strings"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"float('i-am-a-string')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"float('inf')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"float('inf') > 6"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Same hash in a dict"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"a={}\n",
"a[1.0]= 'quora'\n",
"a[True] = 'twitter'\n",
"a[1]= 'facebook'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"print a[True]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"hash(1) == hash(1.0)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Integers in Python"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"a = 25\n",
"b = 25"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"print id(a)\n",
"print id(b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a = 300\n",
"b = 300"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"print id(a)\n",
"print id(b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Local Variable Optimization\n",
"\n",
"> This flaw bites people fairly often. Consider the following function; what do you think happens when you run it?:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"i=1\n",
"def f():\n",
" #global i\n",
" print \"i=\",i\n",
" i = i + 1 \n",
"\n",
"f()"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Trailing spaces"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"for i in range(10): print \"*\","
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"import sys\n",
"for i in range(10): sys.stdout.write(\"*\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Catching Multiple Exceptions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"try:\n",
" #something that raises an error...\n",
"except IndexError, ValueError:\n",
" # expects to catch IndexError and ValueError\n",
" # wrong!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"# correct\n",
"try:\n",
" 1/0\n",
"except (ZeroDivisionError, IndexError, ValueError), e:\n",
" print e\n",
" # integer division or modulo by zero"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# The += operator\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"```python\n",
"x += 42\n",
"\n",
"x = x + 42\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"```python\n",
"a = 1\n",
"a = a + 42\n",
"# a is 43\n",
"a = 1\n",
"a += 42\n",
"# a is 43\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"z = [1, 2, 3]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"z += [4]\n",
"id(z)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"z = z + [5]\n",
"id(z)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Class attributes vs instance attributes"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"```python\n",
">>> class Foo:\n",
"... a = 42\n",
"... def __init__(self):\n",
"... self.a = 43\n",
"... \n",
">>> f = Foo()\n",
">>> f.a\n",
"43\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"```python\n",
">>> class Foo:\n",
"... a = 42\n",
"... \n",
">>> f = Foo()\n",
">>> f.a\n",
"42\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"outputs": [],
"source": [
"class Foo:\n",
" bar = []\n",
" def __init__(self, x):\n",
" self.bar += [x]\n",
"# self.bar = self.bar + [x]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"f = Foo(42)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[42]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f.bar"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true,
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [],
"source": [
"g = Foo(100)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"[42, 100]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"g.bar"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# List slicing\n",
"\n",
"```python\n",
">>> x = [10, 20, 30, 40, 50]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = [10, 20, 30, 40, 50]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x[6:]"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# Mutable default arguments"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"```python\n",
">>> def myfunc(x=[]):\n",
"... x.append(222)\n",
"... print x\n",
"\n",
">>> myfunc([1, 2, 3])\n",
"[1, 2, 3, 222]\n",
"\n",
">>> x = [1, 2]\n",
"\n",
">>> myfunc(x)\n",
"[1, 2, 222]\n",
"\n",
">>> x\n",
"[1, 2, 222]\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"### This was expected. But now .. "
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"```python\n",
">>> myfunc()\n",
"[222]\n",
"\n",
">>> myfunc()\n",
"[222, 222]\n",
"\n",
">>> myfunc()\n",
"[222, 222, 222]\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# String concatenation"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"To illustrate this, a simple benchmark. (timeit is a simple function that runs another function and returns how long it took to complete, in seconds.)\n",
"\n",
"```python\n",
">>> def f():\n",
"... s = \"\"\n",
"... for i in range(100000):\n",
"... s = s + \"abcdefg\"[i % 7]\n",
"... \n",
">>> timeit(f)\n",
"23.7819999456\n",
"\n",
">>> def g():\n",
"... z = []\n",
"... for i in range(100000):\n",
"... z.append(\"abcdefg\"[i % 7])\n",
"... return ''.join(z)\n",
"... \n",
">>> timeit(g)\n",
"0.343000054359\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# QUICK QUESTION\n",
"\n",
"```python\n",
">>> t = (1,2,[1,2,3],)\n",
">>> t[2] += [4, 5]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t = (1,2,[1,2,3],)\n",
"t[2] += [4,5]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# [Tip] Ask for forgiveness than permission"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Don't :\n",
"```python\n",
"if os.path.isfile(file_path) :\n",
" file = open(file_path)\n",
"else :\n",
" # do something\n",
" ```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"Do :\n",
"```python\n",
"try :\n",
" file = open(file_path)\n",
"except OSError as e:\n",
" # do something\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"source": [
"## Resources\n",
"- Reasons for the gotchas above: https://gist.github.com/manojpandey/41b90cba1fd62095e247d1b2448ef85b\n",
"- 'A \"Best of the Best Practices\" (BOBP) guide to developing in Python.:'https://gist.github.com/sloria/7001839\n",
"- Brief explanation for the 'same hash in a dict' gotcha: https://dbader.org/blog/python-mystery-dict-expression\n",
"- Tuple mystery: https://stackoverflow.com/a/29747287/2124480\n",
"- https://docs.python.org/2/whatsnew/2.6.html#other-language-changes\n",
"- https://waymoot.org/home/python_string/"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"# QUESTIONS ? O.o"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Twitter: @onlyrealmvp"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment