You are on page 1of 119

90% of Python in 90 Minutes

90% of Python in 90 Minutes


@__mharrison__ @__mharrison__
Copyright 2013 Copyright 2013

About Me
About Me

12+ years Python


12+ years Python

Worked in Data Analysis, HA, Search,


Worked in Data Analysis, HA, Search,
Open Source, BI, and Storage
Open Source, BI, and Storage

Author of multiple Python Books


Author of multiple Python Books

Goal
Goal

Read Python
Read Python

Write Python
Write Python

Book
Book

Book
Book
Treading on Python Volume 1
Treading on Python Volume 1
covers this
covers this
talk in much more detail.
talk in much more detail.

Begin
Begin

Disclaimer
Disclaimer

Assume some programming experience


Assume some programming experience

Not covering all api's, just syntax


Not covering all api's, just syntax

Warning
Warning

Starting from ground zero


Starting from ground zero

Hands-on at end
Hands-on at end

Three Python'isms to Remember
Three Python'isms to Remember

dir
dir

help
help

colon/indent shuffle
colon/indent shuffle

Why Python?
Why Python?
Python is a powerful, multi-paradigm,
Python is a powerful, multi-paradigm,
interpreted language popular with
interpreted language popular with
start-ups and large Cos
start-ups and large Cos

Python 2 or 3?
Python 2 or 3?
For beginners there is no real difference
For beginners there is no real difference
between Python 2 & 3. The basics are the
between Python 2 & 3. The basics are the
same (except for
same (except for
print
print
)
)

Hello World
Hello World

hello world
hello world
print
print
"hello world"
"hello world"

from interpreter
from interpreter
$ python
$ python
>>>
>>>
print
print
"hello world"
"hello world"
hello world
hello world

REPL
REPL
Read, Eval, Print, Loop
Read, Eval, Print, Loop

REPL
REPL
$ python
$ python
>>>
>>>
2
2
+
+
2
2


# read, eval
# read, eval
4 # print
4 # print
>>>
>>>


# repeat (loop)
# repeat (loop)

REPL (2)
REPL (2)
Many developers keep a REPL handy
Many developers keep a REPL handy
during programming
during programming

From script
From script
Make file
Make file
hello.py
hello.py
with
with
print "hello world"
print "hello world"
Run with:
Run with:
python hello.py
python hello.py

(unix) script
(unix) script
Make file
Make file
hello
hello
with
with
#!/usr/bin/env python
#!/usr/bin/env python
print "hello world"
print "hello world"
Run with:
Run with:
chmod +x hello
chmod +x hello
./hello
./hello

Python 3 hello world
Python 3 hello world
print
print
is no longer a statement, but a
is no longer a statement, but a
function
function
print("hello world")
print("hello world")

Objects
Objects

Objects
Objects
Everything in
Everything in
Python
Python
is an object that has:
is an object that has:

an
an
identity
identity
(
(
id
id
)
)

a
a
value
value
(mutable or immutable)
(mutable or immutable)

id
id
>>>
>>>


!
!
4
4
>>>
>>>
id
id
()
()
"4#"$%"
"4#"$%"

Value
Value

Mutable:
Mutable:
When you alter the item, the
When you alter the item, the
id
id
is still the same. Dictionary, List
is still the same. Dictionary, List

Immutable:
Immutable:
String, Integer, Tuple
String, Integer, Tuple

Mutable
Mutable
>>>
>>>
b
b
!
!
&'
&'
>>>
>>>
id
id
(b)
(b)
(4#")*"#*442###
(4#")*"#*442###
>>>
>>>
b
b
.
.
ppend(
ppend(
+
+
)
)
>>>
>>>
b
b
&+'
&+'
>>>
>>>
id
id
(b)
(b)
(4#")*"#*442### # ,-./!
(4#")*"#*442### # ,-./!

Immutable
Immutable
>>>
>>>


!
!
4
4
>>>
>>>
id
id
()
()
"4#"$%"
"4#"$%"
>>>
>>>


!
!


+
+
(
(
>>>
>>>
id
id
()
()
"4#"$)2 # 0122/3/45!
"4#"$)2 # 0122/3/45!

Variables
Variables


!
!
4
4


# Integer
# Integer
b
b
!
!
*."
*."


# Float
# Float
c
c
!
!
"hello"
"hello"


# String
# String


!
!
"4"
"4"


# rebound to String
# rebound to String

Naming
Naming

lowercase
lowercase

underscore_between_words
underscore_between_words

don't start with numbers


don't start with numbers
See PEP 8
See PEP 8

PEP
PEP
Python Enhancement Proposal (similar to
Python Enhancement Proposal (similar to
JSR in Java)
JSR in Java)

Math
Math

Math
Math
+
+
,
,
6
6
,
,
7
7
,
,
/
/
,
,
77
77
(power),
(power),
8
8
(modulo)
(modulo)

Careful with integer division
Careful with integer division
>>>
>>>
+/4
+/4
#
#
>>>
>>>
+/4.
+/4.
#.)*
#.)*
(In Python 3
(In Python 3
//
//
is integer division
is integer division
operator)
operator)

What happens when you
What happens when you
raise 10 to the 100th?
raise 10 to the 100th?

Long
Long
>>>
>>>
(#77(##
(#77(##
(###############################
(###############################
################################
################################
################################
################################
#####9
#####9

Long
Long
(2)
(2)
>>>
>>>
import
import
sys
sys
>>>
>>>
sys
sys
.
.
mxint
mxint
%22++)2#+"$*4))*$#)
%22++)2#+"$*4))*$#)
>>>
>>>
sys
sys
.
.
mxint
mxint
+
+
(
(
%22++)2#+"$*4))*$#$9
%22++)2#+"$*4))*$#$9

Strings
Strings

Strings
Strings
nme
nme
!
!
:mtt:
:mtt:
with;<uote
with;<uote
!
!
"1 in:t =onn"
"1 in:t =onn"
lon=er
lon=er
!
!
"""5his strin= hs
"""5his strin= hs
multiple lines
multiple lines
in it"""
in it"""

How do I print?
How do I print?
He said, Im sorry
He said, Im sorry

String escaping
String escaping
Escape with
Escape with
>
>
>>> >>> print print :?e sid@ "1 :?e sid@ "1\' \'m sorry": m sorry":
?e sid@ "1:m sorry" ?e sid@ "1:m sorry"
>>> >>> print print :::?e sid@ "1:m sorry"::: :::?e sid@ "1:m sorry":::
?e sid@ "1:m sorry" ?e sid@ "1:m sorry"
>>> >>> print print """?e sid@ "1:m sorry """?e sid@ "1:m sorry\" \"""" """
?e sid@ "1:m sorry" ?e sid@ "1:m sorry"

String escaping (2)
String escaping (2)
Escape Sequence Output
>> Backslash
>: Single quote
>" Double quote
>b ASCII Backspace
>n Newline
>t Tab
>u(2A Unicode 1 bit
>B(2A$%bc Unicode !" bit
>o$4 Octal cha#acte#
>x22 $e% cha#acte#

String formatting
String formatting
c-like
c-like
>>> >>> " "%s %s %s %s" " 8 8( (:hello: :hello:@ @ :world: :world:) )
:hello world: :hello world:
PEP 3101 style
PEP 3101 style
>>> >>> "C#D C(D" "C#D C(D". .Aormt( Aormt(:hello: :hello:@ @ :world: :world:) )
:hello world: :hello world:

Methods &
Methods &
dir
dir

dir
dir
Lists attributes and methods:
Lists attributes and methods:
>>> >>> dir dir( (" strin=" " strin=") )
&:;;dd;;:@ :;;clss;;:@ ... :strtswith:@ :strip:@ &:;;dd;;:@ :;;clss;;:@ ... :strtswith:@ :strip:@
:swpcse:@ :title:@ :trnslte:@ :upper:@ :EAill:' :swpcse:@ :title:@ :trnslte:@ :upper:@ :EAill:'

Whats with all the
Whats with all the
:;;blh;;:
:;;blh;;:
?
?

dunder
dunder
methods
methods
dunder
dunder
(double under) or "special/magic"
(double under) or "special/magic"
methods determine what will happen
methods determine what will happen
when
when
+
+
(
(
;;dd;;
;;dd;;
) or
) or
/
/
(
(
;;div;;
;;div;;
) is
) is
called.
called.

help
help
>>>
>>>
help(
help(
" strin="
" strin="
.
.
strtswith)
strtswith)
?elp on built6in Aunction strtswithF ?elp on built6in Aunction strtswithF
strtswith(...) strtswith(...)
,.strtswith(preAix&@ strt&@ end'') 6G bool ,.strtswith(preAix&@ strt&@ end'') 6G bool
3eturn 5rue iA , strts with the speciAied preAix@ 2lse 3eturn 5rue iA , strts with the speciAied preAix@ 2lse
otherwise. otherwise.
Hith optionl strt@ test , be=innin= t tht position. Hith optionl strt@ test , be=innin= t tht position.
Hith optionl end@ stop comprin= , t tht position. Hith optionl end@ stop comprin= , t tht position.
preAix cn lso be tuple oA strin=s to try. preAix cn lso be tuple oA strin=s to try.

String methods
String methods

s.endswith(sub)
s.endswith(sub)
Returns True if endswith
Returns True if endswith
sub
sub

s.find(sub)
s.find(sub)
Returns index of
Returns index of
sub
sub
or
or
6(
6(

s.format(*args)
s.format(*args)
Places args in string
Places args in string

String methods (2)
String methods (2)

s.index(sub)
s.index(sub)
Returns index of
Returns index of
sub
sub
or exception
or exception

s.join(list)
s.join(list)
Returns
Returns
list
list
items separated by string
items separated by string

s.strip()
s.strip()
Removes whitespace from start/end
Removes whitespace from start/end

Comments
Comments

comments
comments
Comments follow a
Comments follow a
#
#

comments
comments
No multi-line comments
No multi-line comments

More Types
More Types

4one
4one
Pythonic way of saying
Pythonic way of saying
4B99
4B99
. Evaluates
. Evaluates
to False.
to False.
c
c
!
!
4one
4one

booleans
booleans


!
!
5rue
5rue
b
b
!
!
2lse
2lse

sequences
sequences

lists
lists

tuples
tuples

sets
sets

lists
lists
Hold sequences.
Hold sequences.
How would we find out the attributes &
How would we find out the attributes &
methods of a list?
methods of a list?

lists
lists
>>> >>> dir dir(&') (&')
&:;;dd;;:@ :;;clss;;:@ :;;contins;;:@... &:;;dd;;:@ :;;clss;;:@ :;;contins;;:@...
:;;iter;;:@... :;;len;;:@... @ :ppend:@ :count:@ :;;iter;;:@... :;;len;;:@... @ :ppend:@ :count:@
:extend:@ :index:@ :insert:@ :pop:@ :remove:@ :extend:@ :index:@ :insert:@ :pop:@ :remove:@
:reverse:@ :sort:' :reverse:@ :sort:'

lists
lists
>>>
>>>


!
!
&'
&'
>>>
>>>

.
.
ppend(
ppend(
4
4
)
)
>>>
>>>

.
.
ppend(
ppend(
:hello:
:hello:
)
)
>>>
>>>

.
.
ppend(
ppend(
(
(
)
)
>>>
>>>

.
.
sort()
sort()
# in place
# in place
>>>
>>>
print
print

&(@ 4@ :hello:'
&(@ 4@ :hello:'

lists
lists
How would we find out documentation
How would we find out documentation
for a method?
for a method?

lists
lists
help
help
function:
function:
>>> >>> help(&' help(&'. .ppend) ppend)
?elp on built6in Aunction ppendF ?elp on built6in Aunction ppendF
ppend(...) ppend(...)
9.ppend(obIect) 66 ppend obIect to end 9.ppend(obIect) 66 ppend obIect to end

List methods
List methods

l.append(x)
l.append(x)
Insert
Insert
x
x
at end of list
at end of list

l.extend(l)
l.extend(l)
Add
Add
l2
l2
items to list
items to list

l.sort()
l.sort()
In place sort
In place sort

List methods (2)
List methods (2)

l.re!erse()
l.re!erse()
Reverse list in place
Reverse list in place

l.remo!e(item)
l.remo!e(item)
Remove first
Remove first
item
item
found
found

l.pop()
l.pop()
Remove/return item at end of list
Remove/return item at end of list

Dictionaries
Dictionaries

dictionaries
dictionaries
Also called
Also called
hashmap
hashmap
or
or
associative array
associative array

elsewhere
elsewhere
>>>
>>>
=e
=e
!
!
CD
CD
>>>
>>>
=e&
=e&
:=eor=e:
:=eor=e:
'
'
!
!
(#
(#
>>>
>>>
=e&
=e&
:Ared:
:Ared:
'
'
!
!
(2
(2
>>>
>>>
=e&
=e&
:henry:
:henry:
'
'
!
!
(#
(#
>>>
>>>
print
print
=e&
=e&
:=eor=e:
:=eor=e:
'
'
(#
(#

dictionaries
dictionaries
(2)
(2)
Find out if
Find out if
:mtt:
:mtt:
in
in
=e
=e
>>>
>>>
:mtt:
:mtt:
in
in
=e
=e
2lse
2lse

in
in
statement
statement
Uses
Uses
;;contins;;
;;contins;;
dunder method to
dunder method to
determine membership. (Or
determine membership. (Or
;;iter;;
;;iter;;
as
as
fallback)
fallback)

.=et
.=et
>>> >>> print print =e& =e&:chrles: :chrles:' '
5rcebcJ (most recent cll lst)F 5rcebcJ (most recent cll lst)F
2ile 2ile "KstdinG" "KstdinG"@ line @ line ( (@ in KmoduleG @ in KmoduleG
Ley/rror Ley/rrorF :chrles: F :chrles:
>>> >>> print print =e =e. .=et( =et(:chrles: :chrles:@ @ :4ot Aound: :4ot Aound:) )
4ot Aound 4ot Aound

deleting keys
deleting keys
Removing
Removing
:chrles:
:chrles:
from
from
=e
=e
GGG
GGG
del
del
=e&
=e&
:chrles:
:chrles:
'
'

deleting keys
deleting keys
del
del
not in
not in
dir
dir
.
.
.pop
.pop
is an alternative
is an alternative

Functions
Functions

functions
functions
def
def
dd;2
dd;2
(num)F
(num)F


""" return 2
""" return 2
more than num
more than num
"""
"""


return
return
num
num
+
+
2
2
Aive
Aive
!
!
dd;2(
dd;2(
+
+
)
)

functions (2)
functions (2)

deA deA

function name function name

(parameters) (parameters)

F F + indent + indent

optional documentation optional documentation

body body

return return

whitespace
whitespace
Instead of
Instead of
C
C
use a
use a
F
F
and indent
and indent
consistently (4 spaces)
consistently (4 spaces)

whitespace (2)
whitespace (2)
invoke
invoke
python 6tt
python 6tt
to error out during
to error out during
inconsistent tab/space usage in a file
inconsistent tab/space usage in a file

default (named) parameters
default (named) parameters
def
def
dd;n
dd;n
(num@ n
(num@ n
!+
!+
)F
)F


"""default to
"""default to
adding 3"""
adding 3"""


return
return
num
num
+
+
n
n
Aive
Aive
!
!
dd;n(
dd;n(
2
2
)
)
ten
ten
!
!
dd;n(
dd;n(
(*
(*
@
@
6*
6*
)
)

;;doc;;
;;doc;;
Functions have
Functions have
docstrings
docstrings
. Accessible
. Accessible
via
via
.;;doc;;
.;;doc;;
or
or
help
help

;;doc;;
;;doc;;
>>> >>> def def echo echo(txt)F (txt)F
... ... "echo bcJ txt" "echo bcJ txt"
... ... return return txt txt
>>> >>> help(echo) help(echo)
?elp on Aunction echo in module ;;min;;F ?elp on Aunction echo in module ;;min;;F
KM9-4L914/G KM9-4L914/G
echo(txt) echo(txt)
echo bcJ txt echo bcJ txt
KM9-4L914/G KM9-4L914/G

naming
naming

lowercase
lowercase

underscore_between_words
underscore_between_words

don't start with numbers


don't start with numbers

verb
verb
See PEP 8
See PEP 8

Conditionals
Conditionals

conditionals
conditionals
if
if
=rde
=rde
G
G
%#
%#
F
F


print
print
"-"
"-"
elif
elif
=rde
=rde
G
G
$#
$#
F
F


print
print
"M"
"M"
elif
elif
=rde
=rde
G
G
)#
)#
F
F


print
print
"N"
"N"
else
else
F
F


print
print
"0"
"0"

Remember the
Remember the
colon/whitespace!
colon/whitespace!

Booleans
Booleans


!
!
5rue
5rue
b
b
!
!
2lse
2lse

Comparison Operators
Comparison Operators
Supports (
Supports (
G
G
,
,
G!
G!
,
,
K
K
,
,
K!
K!
,
,
!!
!!
,
,
!!
!!
)
)
>>>
>>>
*
*
G
G
%
%
2lse
2lse
>>>
>>>
:mtt:
:mtt:
!!
!!
:Ared:
:Ared:
5rue
5rue
>>>
>>>
isinstnce
isinstnce
(
(
:mtt:
:mtt:
@
@
bsestrin=
bsestrin=
)
)
5rue
5rue

Boolean Operators
Boolean Operators
nd
nd
,
,
or
or
,
,
not
not
(for logical),
(for logical),
O
O
,
,
P
P
, and
, and
Q
Q
(for
(for
bitwise)
bitwise)
>>>
>>>
x
x
!
!
*
*
>>>
>>>
x
x
K
K
64
64
or
or
x
x
G
G
4
4
5rue
5rue

Boolean note
Boolean note
Parens are only required for precedence
Parens are only required for precedence
if
if
(x
(x
G
G
(#
(#
)F
)F


print
print
"Mi="
"Mi="
same as
same as
if
if
x
x
G
G
(#
(#
F
F


print
print
"Mi="
"Mi="

Chained comparisons
Chained comparisons
if
if
+
+
K
K
x
x
K
K
*
*
F
F


print
print
"2our!"
"2our!"
Same as
Same as
if
if
x
x
G
G
+
+
and
and
x
x
K
K
*
*
F
F


print
print
"2our!"
"2our!"

Iteration
Iteration

iteration
iteration
for
for
number
number
in
in
&
&
(
(
@
@
2
2
@
@
+
+
@
@
4
4
@
@
*
*
@
@
"
"
'F
'F


print
print
number
number
for
for
number
number
in
in
rn=e
rn=e
(
(
(
(
@
@
)
)
)F
)F


print
print
number
number

rn=e
rn=e
note
note
Python tends to follow
Python tends to follow
half-open interval
half-open interval

(
(
&strt@end)
&strt@end)
) with
) with
rn=e
rn=e
and slices.
and slices.

end - start = length


end - start = length

easy to concat ranges w/o overlap


easy to concat ranges w/o overlap

iteration (2)
iteration (2)
Java/C-esque style of object in array
Java/C-esque style of object in array
access (BAD):
access (BAD):
nimls nimls ! ! & &"ct" "ct"@ @ "do=" "do="@ @ "bird" "bird"' '
for for index index in in rn=e rn=e( (len len(nimls))F (nimls))F
print print index@ nimls&index' index@ nimls&index'

iteration (3)
iteration (3)
If you need indices, use
If you need indices, use
enumerte
enumerte
nimls nimls ! ! & &"ct" "ct"@ @ "do=" "do="@ @ "bird" "bird"' '
for for index@ vlue index@ vlue in in enumerte enumerte(nimls)F (nimls)F
print print index@ vlue index@ vlue

iteration (4)
iteration (4)
Can
Can
breJ
breJ
out of nearest loop
out of nearest loop
for for item item in in se<uenceF se<uenceF
# process until first negative # process until first negative
if if item item K K # #F F
brea" brea"
# process item # process item

iteration (5)
iteration (5)
Can
Can
continue
continue
to skip over items
to skip over items
for
for
item
item
in
in
se<uenceF
se<uenceF


if
if
item
item
K
K
#
#
F
F


#ontinue
#ontinue


# process all positive items
# process all positive items

iteration (6)
iteration (6)
Can loop over lists, strings, iterators,
Can loop over lists, strings, iterators,
dictionaries... sequence like things:
dictionaries... sequence like things:
my;dict my;dict ! ! C C "nme" "nme"F F "mtt" "mtt"@ @ "csh" "csh"F F *.4* *.4*D D
for for Jey Jey in in my;dict my;dict. .Jeys()F Jeys()F
# process ke # process ke
for for vlue vlue in in my;dict my;dict. .vlues()F vlues()F
# process value # process value
for for Jey@ vlue Jey@ vlue in in my;dict my;dict. .items()F items()F
# process items # process items

pss
pss
pss
pss
is a null operation
is a null operation
for
for
i
i
in
in
rn=e
rn=e
(
(
(#
(#
)F
)F


# do nothing !" times
# do nothing !" times


pass
pass

Hint
Hint
Don't modify
Don't modify
list
list
or
or
dictionary
dictionary
contents
contents
while looping over them
while looping over them

Slicing
Slicing

Slicing
Slicing
Sequences (lists, tuples, strings, etc) can
Sequences (lists, tuples, strings, etc) can
be
be
sliced
sliced
to pull out a single item
to pull out a single item
my;pets
my;pets
!
!
&
&
"do="
"do="
@
@
"ct"
"ct"
@
@
"bird"
"bird"
'
'
Avorite
Avorite
!
!
my;pets&
my;pets&
#
#
'
'
bird
bird
!
!
my;pets&
my;pets&
6(
6(
'
'

Negative Indexing
Negative Indexing
Proper way to think of [negative
Proper way to think of [negative
indexing] is to reinterpret
indexing] is to reinterpret
&6R'
&6R'
as
as
&len()6R'
&len()6R'
@gvanrossum
@gvanrossum

Slicing (2)
Slicing (2)
Slices can take an end index, to pull out a
Slices can take an end index, to pull out a
list of items
list of items
my;pets
my;pets
!
!
&
&
"do="
"do="
@
@
"ct"
"ct"
@
@
"bird"
"bird"
'
'
# a list
# a list
ct;nd;do=
ct;nd;do=
!
!
my;pets&
my;pets&
#
#
F
F
2
2
'
'
ct;nd;do=2
ct;nd;do=2
!
!
my;pets&F
my;pets&F
2
2
'
'
ct;nd;bird
ct;nd;bird
!
!
my;pets&
my;pets&
(
(
F
F
+
+
'
'
ct;nd;bird2
ct;nd;bird2
!
!
my;pets&
my;pets&
(
(
F'
F'

Slicing (3)
Slicing (3)
Slices can take a stride
Slices can take a stride
my;pets
my;pets
!
!
&
&
"do="
"do="
@
@
"ct"
"ct"
@
@
"bird"
"bird"
'
'
# a list
# a list
do=;nd;bird
do=;nd;bird
!
!
&
&
#
#
F
F
+
+
F
F
2
2
'
'
Eero;three;etc
Eero;three;etc
!
!
rn=e
rn=e
(
(
#
#
@
@
(#
(#
)
)
&FF
&FF
+
+
'
'

Slicing (4)
Slicing (4)
Just to beat it in
Just to beat it in
ve=
ve=
!
!
"tomtoe"
"tomtoe"
correct
correct
!
!
ve=&F
ve=&F
6(
6(
'
'
tmte
tmte
!
!
ve=&FF
ve=&FF
2
2
'
'
eotmot
eotmot
!
!
ve=&FF
ve=&FF
6(
6(
'
'

File IO
File IO

File Input
File Input
Open a file to read from it (old style):
Open a file to read from it (old style):
Ain
Ain
!
!
open
open
(
(
"Aoo.txt"
"Aoo.txt"
)
)
for
for
line
line
in
in
AinF
AinF


# manipulate line
# manipulate line
Ain
Ain
.
.
close()
close()

File Output
File Output
Open a file using
Open a file using
:w:
:w:
to
to
write
write
to a file:
to a file:
Aout
Aout
!
!
open
open
(
(
"br.txt"
"br.txt"
@
@
"w"
"w"
)
)
Aout
Aout
.
.
write(
write(
"hello world"
"hello world"
)
)
Aout
Aout
.
.
close()
close()

Always remember to
Always remember to
close your files!
close your files!

closing with
closing with
with
with
implicit
implicit
close
close
(new 2.5+ style)
(new 2.5+ style)
with
with
open
open
(
(
:br.txt:
:br.txt:
)
)
as
as
AinF
AinF


for
for
line
line
in
in
AinF
AinF


# process line
# process line

Classes
Classes

Classes
Classes
#lass #lass $nimal $nimal( (obIect obIect)F )F
def def ;;init;; ;;init;;( (selA selA@ nme)F @ nme)F
selA selA. .nme nme ! ! nme nme
def def tlJ tlJ( (selA selA)F )F
print print "Seneric -niml ,ound" "Seneric -niml ,ound"
niml niml ! ! -niml( -niml("thin=" "thin=") )
niml niml. .tlJ() tlJ()

Classes (2)
Classes (2)
notes:
notes:

obIect
obIect
(base class) (fixed in 3.X)
(base class) (fixed in 3.X)

dunder
dunder
init (constructor)
init (constructor)

all methods take


all methods take
selA
selA
as first parameter
as first parameter

Classes(2)
Classes(2)
Subclassing
Subclassing
#lass #lass %at %at(-niml)F (-niml)F
def def tlJ tlJ( (selA selA)F )F
print print : :%s %s sys@ ".eow!": sys@ ".eow!": 8 8 ( (selA selA. .nme) nme)
ct ct ! ! Nt( Nt("Sroucho" "Sroucho") )
ct ct. .tlJ() tlJ() # invoke method # invoke method

Classes(3)
Classes(3)
#lass
#lass
%heetah
%heetah
(Nt)F
(Nt)F


"""classes can have
"""classes can have
docstrings"""
docstrings"""


def
def
tlJ
tlJ
(
(
selA
selA
)F
)F


print
print
"Srowl"
"Srowl"

naming
naming

CamelCase
CamelCase

don't start with numbers


don't start with numbers

Nouns
Nouns

Debugging
Debugging

Poor mans
Poor mans
print
print
works a lot of the time
works a lot of the time

Remember
Remember
Clean up
Clean up
print
print
statements. If you really
statements. If you really
need them, use
need them, use
lo==in=
lo==in=
or write to
or write to
sys.stdout
sys.stdout

pdb
pdb
import
import
pdb
pdb
T pdb
T pdb
.
.
set;trce()
set;trce()

pdb
pdb
commands
commands

h
h
- help
- help

s
s
- step into
- step into

n
n
- next
- next

c
c
- continue
- continue

w
w
- where am I (in stack)?
- where am I (in stack)?

l
l
- list code around me
- list code around me

That's all
That's all
Questions? Tweet me
Questions? Tweet me
For more details see
For more details see
Treading on Python Volume 1
Treading on Python Volume 1
@__mharrison__ @__mharrison__
http://hairysun.com http://hairysun.com