When we succeed, they understand:
I have a modest proposal
Imagine the edge of a tall cliff
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Imagine the waves crashing below
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rocks
↓ spray
_ __ waves ↓ _ __
╱ ╲╱ ╲ ∵ ↓ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
↑
thus, danger
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
↑
you could fall!
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
Names are your friends.
a = 3
@ >>> a = 3
┌─┴─┐
└┐a┌┤
│ ││
~~~~~~~└─┘│~~~~~~~~~~~~~~~~~~
╔═╧═╗
║ 3 ║
╚═══╝
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
@ >>> a = 3
┌─┴─┐ >>> del a
└┐a┌┤
│ ││
~~~~~~~└─┘│~~~~~~~~~~~~~~~~~~
╔═╧═╗
║ 3 ║
╚═══╝
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
@ >>> a = 3
┌─┴─┐… >>> del a
└┐a┌┤…
│ ││…
~~└─┘~~~~~~~~~~~~~~~~~~~~~~~~
┊ ┊
╔═╧═╗
║ 3 ║
╚═══╝
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(splash)
∵
_ __ ∵ ∵ _ __
╱ ╲╱ ╲ V ∵ ╱ ╲╱ ╲
╲_╱VVVVVVV VVVVVVVVVVVVVVV╲__╱
@ >>> a = 3
┌─┴─┐
└┐a┌┤
│ ││
~~~~~~~└─┘│~~~~~~~~~~~~~~~~~~
╔═╧═╗
║ 3 ║
╚═══╝
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
@ >>> a = 3
┌─┴─┐ >>> a = a + 1
└┐a┌┤
│ ││
~~~~~~~└─┘│~~~~~~~~~~~~~~~~~~
╔═╧═╗
║ 4 ║
╚═══╝
┊ ┊
╔═╧═╗
║ 3 ║
╚═══╝
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
@ >>> a = 3
┌─┴─┐ >>> a = a + 1
└┐a┌┤
│ ││
~~~~~~~└─┘│~~~~~~~~~~~~~~~~~~
╔═╧═╗
║ 4 ║
╚═══╝
(splash)
∵
_ __ ∵ ∵ _ __
╱ ╲╱ ╲ V ∵ ╱ ╲╱ ╲
╲_╱VVVVVVV VVVVVVVVVVVVVVV╲__╱
@ >>> for a in range(5):
┌─┴─┐ ... print a
└┐a┌┤
│ ││
~~~~~~~└─┘│~~~~~~~~~~~~~~~~~~
╔═╧═╗
║ 4 ║
╚═══╝
┊ ┊
╔═╧═╗
║ 3 ║
╚═══╝
┊ ┊
╔═╧═╗
║ 2 ║
_ __ ╚═══╝ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
@ >>> a = {3,4,5}
┌─┴─┐ >>> 4 in a
└┐a┌┤ True
│ ││
~~~~~~~└─┘│~~~~~~~~~~~~~~~~~~
╔════╧══════╗
║ set ║
╚╤════╤════╤╝
╔═╧═╗╔═╧═╗╔═╧═╗
║ 3 ║║ 4 ║║ 5 ║
╚═══╝╚═══╝╚═══╝
Sets are the simplest
All the objects dangling
are "in" the set
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
@ >>> a = [3,4,5]
┌─┴─┐ >>> a[2]
└┐a┌┤ 5
│ ││
~~~~~~~└─┘│~~~~~~~~~~~~~~~~~~
╔═════╧═══════╗
║ list ║
╚0╤═══1╤═══2╤═╝
╔═╧═╗╔═╧═╗╔═╧═╗
║ 3 ║║ 4 ║║ 5 ║
╚═══╝╚═══╝╚═══╝
Lists are more complex
Each object lives at a
particular index
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
@ >>> a = {3: 9,
┌─┴─┐ ... 4: 16}
└┐a┌┤
│ ││
~~~~~~~└─┘│~~~~~~~~~~~~~~~~~~
╔═══╧══════╗
║ dict ║
╚═╤═══════╤╝
╔═══╗│ ╔═══╗│
║ 3 ╟┤ ║ 4 ╟┤
╚══╔═╧═╗╚══╔═╧══╗
║ 9 ║ ║ 16 ║
╚═══╝ ╚════╝
Dictionaries are most complex
Two sub-objects: key AND value
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
class Person(object):
pass
@
┌─┴─┐ b = Person()
├┐b┌┘ b.name = a
││ │ b.age = 3
~~~~~~~~~~~│└─┘~~~~~~~~~~~~~~
╔═══╧════╗
║ Person ║
║name age║
╚══╪═══╪═╝
╔═════╧═╗╔╧══╗
║brandon║║ 3 ║
╚═══════╝╚═══╝
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
Have the beginner try this
class Person(object):
pass
a = 'brandon'
b = Person()
b.name = a
b.age = 3
@ @ a = 'brandon'
┌─┴─┐ ┌─┴─┐ b = Person()
├┐a┌┘ ├┐b┌┘ b.name = a
││ │ ││ │ b.age = 3
~~~~│└─┘~~~│└─┘~~~~~~~~~~~~~~
│ ╔═══╧════╗
│ ║ Person ║
│ ║name age║
│ ╚══╪═══╪═╝
╔╧═════╧╗╔═╧═╗
║brandon║║ 3 ║
╚═══════╝╚═══╝
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
@ @ a = 'brandon'
┌─┴─┐ ┌─┴─┐ b = Person()
├┐a┌┘ ├┐b┌┘ b.name = a
││ │ ││ │ b.age = 3
~~~~│└─┘~~~│└─┘~~~~~~~~~~~~~~
│ ╔═══╧════╗
│ ║ Person ║
│ ║name age║
│ ╚══╪═══╪═╝
╔╧═════╧╗╔═╧═╗
║brandon║║ 3 ║
╚═══════╝╚═══╝
“What happens if we ‘del b’?”
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
@ @
┌─┴─┐ ┌─┴─┐ del b
├┐a┌┘ ├┐b┌┘
││ │ │ │
~~~~│└─┘~~~~~└─┘~~~~~~~~~~~~~~
╔╧══════╗
║brandon║
╚═══════╝
┊ ┊
╔═══╧════╗
║ Person ║
║name age║
╚══════╪═╝
╔═╧═╗
_ __ ║ 3 ║ _ __
╱ ╲╱ ╲ ∵ ╚═══╝ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
Beginners get confused
>>> a = [3, 4, 5]
>>> b = a
>>> b # no surprise
[3, 4, 5]
>>> b.append(6)
>>> b # no surprise
[3, 4, 5, 6]
But, what is the value of a?
@ >>> a = [3,4,5]
┌─┴─┐
└┐a┌┤
│ ││
~~~└─┘│~~~~~~~~~~~~~~~~~~~~~~
╔═╧═══════════╗
║ list ║
╚0╤═══1╤═══2╤═╝
╔═╧═╗╔═╧═╗╔═╧═╗
║ 3 ║║ 4 ║║ 5 ║
╚═══╝╚═══╝╚═══╝
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
@ @ >>> a = [3,4,5]
┌─┴─┐┌─┴─┐ >>> b = a
└┐a┌┤└┐b┌┤
│ ││ │ ││
~~~└─┘│~└─┘│~~~~~~~~~~~~~~~~~
╔═╧════╧══════╗
║ list ║
╚0╤═══1╤═══2╤═╝
╔═╧═╗╔═╧═╗╔═╧═╗
║ 3 ║║ 4 ║║ 5 ║
╚═══╝╚═══╝╚═══╝
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
@ @ >>> a = [3,4,5]
┌─┴─┐┌─┴─┐ >>> b = a
└┐a┌┤└┐b┌┤ >>> b.append(6)
│ ││ │ ││
~~~└─┘│~└─┘│~~~~~~~~~~~~~~~~~
╔═╧════╧═══════════╗
║ list ║
╚0╤═══1╤═══2╤═══3╤═╝
╔═╧═╗╔═╧═╗╔═╧═╗╔═╧═╗
║ 3 ║║ 4 ║║ 5 ║║ 6 ║
╚═══╝╚═══╝╚═══╝╚═══╝
“What is a?”
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
def isbigger(n, m):
"Is `n` > all values in list `m`?"
m.append(n)
m.sort()
return m[-1] == n
>>> a = [6, 3, 4]
>>> isbigger(8, a)
True
>>> a # ack! it changed!
[3, 4, 6, 8]
def isbigger(n, m):
…
isbigger(8, a)
def isbigger(…):
n = 8
m = a
…
@
┌─┴─┐
└┐a┌┤
│ ││ @ @
~~~└─┘│~ ┌─┴─┐ ~~~~ ┌─┴─┐ ~~ global namespace
│ └┐m┌┤ └┐n┌┤
│ │ ││ │ ││
│ ~~└─┘│~~~~~~~└─┘│~~~ f() namespace
╔═╧══════╧════╗ ╔═╧═╗
║ list ║ ║ 8 ║
╚0╤═══1╤═══2╤═╝ ╚═══╝
╔═╧═╗╔═╧═╗╔═╧═╗
║ 6 ║║ 3 ║║ 4 ║
╚═══╝╚═══╝╚═══╝
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
a = 80 * 24 - 1
@ @
┌─┴─┐ ┌─┴─┐ a = 80 * 24 - 1
├┐P┌┤ ├┐a┌┘
││y││ │ │
~~~~│└─┘│~~~~└─┘~~~~~~~~~~~~~~
╔═╧╗╔═╧╗
║80║║24║
╚══╝╚══╝
@ @
┌─┴─┐ ┌─┴─┐ a = 80 * 24 - 1
├┐P┌┤ ├┐a┌┘
││y││ │ │
~~~~│└─┘│~~~~└─┘~~~~~~~~~~~~~~
╔══╧═╗╔╧╗
║1920║║1║
╚════╝╚═╝
@ @
┌─┴─┐ ┌─┴─┐ a = 80 * 24 - 1
├┐P┌┤ → ├┐a┌┘
│y│ ││ │
~~~~~└─┘~~~~│└─┘~~~~~~~~~~~~~~
╔══╧═╗
║1919║
╚════╝
But when do intermediate results matter?
>>> for a in range(99):
... …
>>> for a in range(100000):
... read_datum()
>>> for a in xrange(100000):
... read_datum()
>>> m = range(100000):
>>> m
[0, 1, 2, 3, 4, ..., 99999]
>>>
>>> m = xrange(100000)
>>> m
xrange(100000)
>>> mi = iter(m)
>>> mi.next()
0
>>> mi.next()
1
only two points here
import sys
means
sys = <the module named "sys">
from sys import version
means
version = <the module named "sys">.version
from sys import version as python_ver
means
python_ver = <the module named "sys">.version
def f(…):
return g(…)
@
┌─┴─┐ f(…)
└┐ ┌┤ @
│ │ ┌─┴─┐
~~~└─┘~~└┐ ┌┤~~ @ ~~~~ global scope
│ │ ┌─┴─┐
~└─┘~~└┐ ┌┤~~~ f() scope
│ │
~~└─┘~~~~ g() scope
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
def isbigger(m, n):
"Is `n` > all values in `m`?"
m.append(myvalue)
m.sort()
return m[-1] == n
>>> a = [6, 3, 4]
>>> isbigger(a, 8)
def isbigger(m, n):
"Is `n` > all values in `m`?"
m.append(myvalue)
m.sort()
print m is a # this happens to work
return m[-1] == n
>>> a = [6, 3, 4]
>>> isbigger(a, 8)
f() can see `a`
@ NOT because `a` is above
┌─┴─┐ BUT because `a` surrounds its code
└┐a┌┤
│ ││ @ @
~~~└─┘│~ ┌─┴─┐ ~~~~ ┌─┴─┐ ~~ global scope
│ └┐m┌┤ └┐n┌┤
│ │ ││ │ ││
│ ~~└─┘│~~~~~~~└─┘│~~~ f() scope
╔═╧══════╧════╗ ╔═╧═╗
║ list ║ ║ 8 ║
╚0╤═══1╤═══2╤═╝ ╚═══╝
╔═╧═╗╔═╧═╗╔═╧═╗
║ 6 ║║ 3 ║║ 4 ║
╚═══╝╚═══╝╚═══╝
_ __ _ __
╱ ╲╱ ╲ ∵ ∵ ╱ ╲╱ ╲
╲_╱VVVVVVVVVVVVVVVVVVVVVVV╲__╱
Small integers are cached
>>> (2 + 1) is (2 + 1)
True
>>> (1000 + 1) is (1000 + 1)
False
>>> 1.0 + 2.0 is 1.0 + 2.0
False
>>> a = 1.0 + 2.0
>>> a
3.0
>>> b = 1.0 + 2.0
>>> b
3.0
>>> a == b
True
>>> id(a)
140202964
>>> id(b)
140202916
>>> a is b
False
Interned strings are cached
>>> 'my string' is 'my string'
True
>>> ('my string' + 'a') is ('my string' + 'a')
False
>>> s1 = intern('my string' + 'a')
>>> s2 = intern('my string' + 'a')
>>> s1 is s2
True
>>> (1, 2, 3) is (1, 2, 3)
True
>>> [1, 2, 3] is [1, 2, 3]
False
A: Yes!
>>> None is None
True
Why should beginners use is?
Cyclic garbage collection
Certainly not important until 4:30pm