Exploring slides

A
Exploring*is*Never*Boring
@akaptur
Exploring*is*Never*Boring
Understanding+CPython+without+
reading+the+code
Why$do$this?
!
Contribu)ng
h"ps://docs.python.org/devguide/
"Read&the&code!"
Exploring slides
Exploring slides
Not$reading$the$code
Strategies
Observa(on
Experimenta+on
Strategy(1:(Observa/on
"Code&is&not&literature&and&we&are&not&readers.&Rather,&
interes4ng&pieces&of&code&are&specimens&and&we&are&
naturalists."
—"Peter"Seibel,"Code"is"not"literature
Exploring slides
How$to$observe
Tools%for%observa,on:%inspect
>>> import random
>>> import inspect
>>> print inspect.getsource(random.choice)
def choice(self, seq):
"""Choose a random element from a non-empty sequence."""
return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
Tools%for%observa,on:%inspect
>>> import random
>>> import inspect
>>> print inspect.getsource(random.choice)
def choice(self, seq):
"""Choose a random element from a non-empty sequence."""
return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
>>> print inspect.getsource(list.append)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 701, in getsource
lines, lnum = getsourcelines(object)
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 690, in getsourcelines
lines, lnum = findsource(object)
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 526, in findsource
file = getfile(object)
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/inspect.py", line 420, in getfile
'function, traceback, frame, or code object'.format(object))
TypeError: <method 'append' of 'list' objects> is not a module, class, method, function, traceback, frame, or code object
>>> # :(
github.com/punchagan/cinspect
Tools%for%observa,on:%history%&%
changelogs
Tools%for%observa,on:%history%&%changelogs
hg blame Python/ceval.c
hg log -r ### -p
Tools%for%observa,on:%internal%
structure
Tools%for%observa,on:%internal%structure
>>> False is False is False
Tools%for%observa,on:%internal%structure
>>> (False is False) is False
Tools%for%observa,on:%internal%structure
>>> False is False is False
True
Tools%for%observa,on:%internal%structure
>>> False is False is False
True
>>> a < b < c
True
Tools%for%observa,on:%internal%structure
1 0 LOAD_NAME 0 (False)
3 LOAD_NAME 0 (False)
6 DUP_TOP
7 ROT_THREE
8 COMPARE_OP 8 (is)
11 JUMP_IF_FALSE_OR_POP 23
14 LOAD_NAME 0 (False)
17 COMPARE_OP 8 (is)
20 JUMP_FORWARD 2 (to 25)
>> 23 ROT_TWO
24 POP_TOP
>> 25 POP_TOP
26 LOAD_CONST 0 (None)
29 RETURN_VALUE
Tools%for%observa,on:%internal%structure
1 0 LOAD_NAME 0 (a)
3 LOAD_NAME 1 (b)
6 DUP_TOP
7 ROT_THREE
8 COMPARE_OP 0 (<)
11 JUMP_IF_FALSE_OR_POP 23
14 LOAD_NAME 2 (c)
17 COMPARE_OP 0 (<)
20 JUMP_FORWARD 2 (to 25)
>> 23 ROT_TWO
24 POP_TOP
>> 25 POP_TOP
26 LOAD_CONST 0 (None)
29 RETURN_VALUE
Strategy(2:(Experimenta1on
Exploring slides
Tools%for%science:%measurements
e.g.$%meit
$ python -m timeit -s "li = range(100)" "li.sort(reverse=True)"
1000000 loops, best of 3: 1.75 usec per loop
$ python -m timeit -s "li = range(100)" "sorted(li, reverse=True)"
100000 loops, best of 3: 2.46 usec per loop
Tools%for%science:%write%tests
Tools%for%science:%simula0ons
Observa(on+&+Experimenta(on+>+Reading+
the+code
• Introspect+it
• Examine+it+cri1cally
• Watch+it+evolve
• Measure+it
• Test+it
• Change+it
• Poke+it+with+a+s1ck
Ques%ons
class Random(_random.Random):
"""Random number generator base class used by bound module functions.
Used to instantiate instances of Random to get generators that don't
share state."""
...
# Create one instance, seeded from current time, and export its methods
# as module-level functions. The functions share state across all uses
#(both in the user's code and in the Python libraries), but that's fine
# for most programs and is easier for the casual user than making them
# instantiate their own Random() instance.
_inst = Random()
seed = _inst.seed
random = _inst.random
randint = _inst.randint
choice = _inst.choice
...
1 of 33

Recommended

Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython by
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonByterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPythonakaptur
2K views40 slides
Diving into byte code optimization in python by
Diving into byte code optimization in python Diving into byte code optimization in python
Diving into byte code optimization in python Chetan Giridhar
4.5K views28 slides
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha... by
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...akaptur
1.3K views48 slides
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!... by
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!..."A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...akaptur
3K views25 slides
Python opcodes by
Python opcodesPython opcodes
Python opcodesalexgolec
5K views25 slides
TCO in Python via bytecode manipulation. by
TCO in Python via bytecode manipulation.TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.lnikolaeva
1.1K views19 slides

More Related Content

What's hot

Introducción a Elixir by
Introducción a ElixirIntroducción a Elixir
Introducción a ElixirSvet Ivantchev
68 views40 slides
The secrets of inverse brogramming by
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogrammingRichie Cotton
1.1K views30 slides
JavaScript @ CTK by
JavaScript @ CTKJavaScript @ CTK
JavaScript @ CTKJakob Mattsson
517 views55 slides
Welcome to python by
Welcome to pythonWelcome to python
Welcome to pythonKyunghoon Kim
911 views47 slides
Implementing Software Machines in C and Go by
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and GoEleanor McHugh
975 views85 slides
Implementing virtual machines in go & c 2018 redux by
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxEleanor McHugh
761 views66 slides

What's hot(20)

The secrets of inverse brogramming by Richie Cotton
The secrets of inverse brogrammingThe secrets of inverse brogramming
The secrets of inverse brogramming
Richie Cotton1.1K views
Implementing Software Machines in C and Go by Eleanor McHugh
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and Go
Eleanor McHugh975 views
Implementing virtual machines in go & c 2018 redux by Eleanor McHugh
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 redux
Eleanor McHugh761 views
Playing 44CON CTF for fun and profit by 44CON
Playing 44CON CTF for fun and profitPlaying 44CON CTF for fun and profit
Playing 44CON CTF for fun and profit
44CON1.4K views
Are we ready to Go? by Adam Dudczak
Are we ready to Go?Are we ready to Go?
Are we ready to Go?
Adam Dudczak1.3K views
Python 101 language features and functional programming by Lukasz Dynowski
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
Lukasz Dynowski1.1K views
Implementing Software Machines in Go and C by Eleanor McHugh
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and C
Eleanor McHugh567 views
When RV Meets CEP (RV 2016 Tutorial) by Sylvain Hallé
When RV Meets CEP (RV 2016 Tutorial)When RV Meets CEP (RV 2016 Tutorial)
When RV Meets CEP (RV 2016 Tutorial)
Sylvain Hallé910 views
1 seaborn introduction by YuleiLi3
1 seaborn introduction 1 seaborn introduction
1 seaborn introduction
YuleiLi3245 views
Python for Scientists by Andreas Dewes
Python for ScientistsPython for Scientists
Python for Scientists
Andreas Dewes1.9K views
A, B, C. 1, 2, 3. Iterables you and me - Willian Martins (ebay) by Shift Conference
A, B, C. 1, 2, 3. Iterables you and me - Willian Martins (ebay)A, B, C. 1, 2, 3. Iterables you and me - Willian Martins (ebay)
A, B, C. 1, 2, 3. Iterables you and me - Willian Martins (ebay)
Shift Conference57 views
โปรแกรมย่อยและฟังชันก์มาตรฐาน by knang
โปรแกรมย่อยและฟังชันก์มาตรฐานโปรแกรมย่อยและฟังชันก์มาตรฐาน
โปรแกรมย่อยและฟังชันก์มาตรฐาน
knang366 views
Parallel Computing in R by mickey24
Parallel Computing in RParallel Computing in R
Parallel Computing in R
mickey241.5K views

Similar to Exploring slides

Extreme Swift by
Extreme SwiftExtreme Swift
Extreme SwiftMovel
1K views58 slides
ES3-2020-06 Test Driven Development (TDD) by
ES3-2020-06 Test Driven Development (TDD)ES3-2020-06 Test Driven Development (TDD)
ES3-2020-06 Test Driven Development (TDD)David Rodenas
63 views58 slides
Python by
PythonPython
Python대갑 김
476 views44 slides
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn by
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnArnaud Joly
3.2K views37 slides
Cpl by
CplCpl
CplAyesha Shariff
64 views3 slides
Introduction to python programming 1 by
Introduction to python programming   1Introduction to python programming   1
Introduction to python programming 1Giovanni Della Lunga
263 views85 slides

Similar to Exploring slides(20)

Extreme Swift by Movel
Extreme SwiftExtreme Swift
Extreme Swift
Movel1K views
ES3-2020-06 Test Driven Development (TDD) by David Rodenas
ES3-2020-06 Test Driven Development (TDD)ES3-2020-06 Test Driven Development (TDD)
ES3-2020-06 Test Driven Development (TDD)
David Rodenas63 views
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn by Arnaud Joly
Numerical tour in the Python eco-system: Python, NumPy, scikit-learnNumerical tour in the Python eco-system: Python, NumPy, scikit-learn
Numerical tour in the Python eco-system: Python, NumPy, scikit-learn
Arnaud Joly3.2K views
Snakes for Camels by miquelruizm
Snakes for CamelsSnakes for Camels
Snakes for Camels
miquelruizm693 views
Python Performance 101 by Ankur Gupta
Python Performance 101Python Performance 101
Python Performance 101
Ankur Gupta3.2K views
The Ring programming language version 1.5.4 book - Part 79 of 185 by Mahmoud Samir Fayed
The Ring programming language version 1.5.4 book - Part 79 of 185The Ring programming language version 1.5.4 book - Part 79 of 185
The Ring programming language version 1.5.4 book - Part 79 of 185
design and analysis of algorithm Lab files by Nitesh Dubey
design and analysis of algorithm Lab filesdesign and analysis of algorithm Lab files
design and analysis of algorithm Lab files
Nitesh Dubey771 views
Esoteric Data structures by Mugisha Moses
Esoteric Data structures Esoteric Data structures
Esoteric Data structures
Mugisha Moses1.3K views
The Ring programming language version 1.5.3 book - Part 89 of 184 by Mahmoud Samir Fayed
The Ring programming language version 1.5.3 book - Part 89 of 184The Ring programming language version 1.5.3 book - Part 89 of 184
The Ring programming language version 1.5.3 book - Part 89 of 184
Introduction to Go by Jaehue Jang
Introduction to GoIntroduction to Go
Introduction to Go
Jaehue Jang1.3K views
Go 프로그래밍 소개 - 장재휴, DomainDriven커뮤니티 by JaeYeoul Ahn
Go 프로그래밍 소개 - 장재휴, DomainDriven커뮤니티Go 프로그래밍 소개 - 장재휴, DomainDriven커뮤니티
Go 프로그래밍 소개 - 장재휴, DomainDriven커뮤니티
JaeYeoul Ahn281 views
Python quickstart for programmers: Python Kung Fu by climatewarrior
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
climatewarrior7.4K views
Python for Scientific Computing by Albert DeFusco
Python for Scientific ComputingPython for Scientific Computing
Python for Scientific Computing
Albert DeFusco2.9K views
Java Generics for Dummies by knutmork
Java Generics for DummiesJava Generics for Dummies
Java Generics for Dummies
knutmork2.9K views

Recently uploaded

Melek BEN MAHMOUD.pdf by
Melek BEN MAHMOUD.pdfMelek BEN MAHMOUD.pdf
Melek BEN MAHMOUD.pdfMelekBenMahmoud
14 views1 slide
Spesifikasi Lengkap ASUS Vivobook Go 14 by
Spesifikasi Lengkap ASUS Vivobook Go 14Spesifikasi Lengkap ASUS Vivobook Go 14
Spesifikasi Lengkap ASUS Vivobook Go 14Dot Semarang
35 views1 slide
Empathic Computing: Delivering the Potential of the Metaverse by
Empathic Computing: Delivering  the Potential of the MetaverseEmpathic Computing: Delivering  the Potential of the Metaverse
Empathic Computing: Delivering the Potential of the MetaverseMark Billinghurst
470 views80 slides
STPI OctaNE CoE Brochure.pdf by
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdfmadhurjyapb
12 views1 slide
Five Things You SHOULD Know About Postman by
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About PostmanPostman
27 views43 slides
Uni Systems for Power Platform.pptx by
Uni Systems for Power Platform.pptxUni Systems for Power Platform.pptx
Uni Systems for Power Platform.pptxUni Systems S.M.S.A.
50 views21 slides

Recently uploaded(20)

Spesifikasi Lengkap ASUS Vivobook Go 14 by Dot Semarang
Spesifikasi Lengkap ASUS Vivobook Go 14Spesifikasi Lengkap ASUS Vivobook Go 14
Spesifikasi Lengkap ASUS Vivobook Go 14
Dot Semarang35 views
Empathic Computing: Delivering the Potential of the Metaverse by Mark Billinghurst
Empathic Computing: Delivering  the Potential of the MetaverseEmpathic Computing: Delivering  the Potential of the Metaverse
Empathic Computing: Delivering the Potential of the Metaverse
Mark Billinghurst470 views
STPI OctaNE CoE Brochure.pdf by madhurjyapb
STPI OctaNE CoE Brochure.pdfSTPI OctaNE CoE Brochure.pdf
STPI OctaNE CoE Brochure.pdf
madhurjyapb12 views
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman27 views
Web Dev - 1 PPT.pdf by gdsczhcet
Web Dev - 1 PPT.pdfWeb Dev - 1 PPT.pdf
Web Dev - 1 PPT.pdf
gdsczhcet55 views
Voice Logger - Telephony Integration Solution at Aegis by Nirmal Sharma
Voice Logger - Telephony Integration Solution at AegisVoice Logger - Telephony Integration Solution at Aegis
Voice Logger - Telephony Integration Solution at Aegis
Nirmal Sharma17 views
Special_edition_innovator_2023.pdf by WillDavies22
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdf
WillDavies2216 views
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Case Study Copenhagen Energy and Business Central.pdf by Aitana
Case Study Copenhagen Energy and Business Central.pdfCase Study Copenhagen Energy and Business Central.pdf
Case Study Copenhagen Energy and Business Central.pdf
Aitana12 views
From chaos to control: Managing migrations and Microsoft 365 with ShareGate! by sammart93
From chaos to control: Managing migrations and Microsoft 365 with ShareGate!From chaos to control: Managing migrations and Microsoft 365 with ShareGate!
From chaos to control: Managing migrations and Microsoft 365 with ShareGate!
sammart939 views
handbook for web 3 adoption.pdf by Liveplex
handbook for web 3 adoption.pdfhandbook for web 3 adoption.pdf
handbook for web 3 adoption.pdf
Liveplex19 views
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi120 views
Black and White Modern Science Presentation.pptx by maryamkhalid2916
Black and White Modern Science Presentation.pptxBlack and White Modern Science Presentation.pptx
Black and White Modern Science Presentation.pptx
maryamkhalid291614 views
Perth MeetUp November 2023 by Michael Price
Perth MeetUp November 2023 Perth MeetUp November 2023
Perth MeetUp November 2023
Michael Price15 views

Exploring slides