Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Python's Functional Tools
Search
Julian Gindi
December 03, 2013
Programming
1
830
Python's Functional Tools
Advanced Python Magic
Julian Gindi
December 03, 2013
Tweet
Share
Other Decks in Programming
See All in Programming
STYLE
koic
0
160
テストやOSS開発に役立つSetup PHP Action
matsuo_atsushi
0
150
リリース時」テストから「デイリー実行」へ!開発マネージャが取り組んだ、レガシー自動テストのモダン化戦略
goataka
0
120
connect-python: convenient protobuf RPC for Python
anuraaga
0
400
AWS CDKの推しポイントN選
akihisaikeda
1
240
開発に寄りそう自動テストの実現
goyoki
1
780
俺流レスポンシブコーディング 2025
tak_dcxi
14
8.5k
社内オペレーション改善のためのTypeScript / TSKaigi Hokuriku 2025
dachi023
1
570
30分でDoctrineの仕組みと使い方を完全にマスターする / phpconkagawa 2025 Doctrine
ttskch
3
800
ハイパーメディア駆動アプリケーションとIslandアーキテクチャ: htmxによるWebアプリケーション開発と動的UIの局所的適用
nowaki28
0
400
これだけで丸わかり!LangChain v1.0 アップデートまとめ
os1ma
6
1.8k
全員アーキテクトで挑む、 巨大で高密度なドメインの紐解き方
agatan
8
20k
Featured
See All Featured
Being A Developer After 40
akosma
91
590k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.8k
Mobile First: as difficult as doing things right
swwweet
225
10k
Leading Effective Engineering Teams in the AI Era
addyosmani
8
1.3k
Navigating Team Friction
lara
191
16k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.2k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
Into the Great Unknown - MozCon
thekraken
40
2.2k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Context Engineering - Making Every Token Count
addyosmani
9
500
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.5k
Transcript
Functional Python Tools Advanced Python Magic
I’m Julian Gindi • Python developer at iStrategylabs (here) •
(small-time) Python core contributor • github.com/juliangindi • @JulianGindi • JulianGindi.com • lebowski.juliangindi.com
Python @isl • Django (content-heavy websites) • General scripting and
internal tools • Fabric (executing commands on our servers) • Salt (provisioning our servers)
Goals • Basic understanding of functional programming • Explain how
it can help you craft simple, yet powerful code • Show you a couple practical applications of the functional paradigm
Golden Circle
Functional Programming (What) • First class functions. Higher-order functions •
Pure functions • State • Immutable Data • Verbs instead of nouns
Python? • Not a functional language in a “pure” sense
• Multi paradigm language • Doesn't really matter - tools are still useful and awesome • Practical
Why? • Downright awesome • Programs are easier to understand
and debug • Often faster • Superior ability to deal with concurrency
How? • Time for some CODE!
>>> my_tuple = ('A', 'B', ‘C’) >>> len(my_tuple) 3 >>>
my_tuple.append('D') Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'tuple' object has no attribute 'append'
Practical Features (Core) • Closures • Lambda’s • Iterators, generators
• Partials • List Comprehension • Tuples • Built in functions (map, reduce, apply)
# List Comprehensions def square_list(items): new_list = [] new_list.append([x
* x for x in items]) return new_list ! print square_list([1,2,3]) ! >> [1, 4, 9]
# Closures ! def average_closure(a, b): def sum(): return a
+ b return sum() / 2 ! print average_closure(4, 4) ! >> 4
import operator ! # Lambdas ! def weird_average(func, data) :
return reduce(operator.add, map(func, data)) / len(data) ! print weird_average(lambda x: x * x , [1,2,3]) ! >> 4.66
# Generators ! def fibonacci() : prev, current = 0,1
while True : yield current prev, current = current, prev + current ! # Generator instantiation gen = fibonacci() ! # Generator usage for i in range(10) : print gen.next() ! >> 1 1 2 3 5 8 13 21 34 55
# PARTIALS ! from functools import partial ! def power(x,y):
return x ** y ! square = partial(power, y = 2) cube = partial(power, y = 3) ! assert (square(5), cube(7)) == (25, 343) ! ! !
How to get started • ‘import functools’ • Create a
simple program in a functional style • http://www.defmacro.org/ ramblings/fp.html - Functional programming for the rest of us • Execution in the Kingdom of Nouns