SlideShare a Scribd company logo
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
...

More Related Content

PDF
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
PDF
Diving into byte code optimization in python
PDF
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
PDF
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
PDF
Python opcodes
PPTX
TCO in Python via bytecode manipulation.
PPT
Python легко и просто. Красиво решаем повседневные задачи
PDF
Let's golang
Byterun, a Python bytecode interpreter - Allison Kaptur at NYCPython
Diving into byte code optimization in python
Allison Kaptur: Bytes in the Machine: Inside the CPython interpreter, PyGotha...
"A 1,500 line (!!) switch statement powers your Python!" - Allison Kaptur, !!...
Python opcodes
TCO in Python via bytecode manipulation.
Python легко и просто. Красиво решаем повседневные задачи
Let's golang

What's hot (20)

PDF
Introducción a Elixir
ODP
The secrets of inverse brogramming
KEY
JavaScript @ CTK
PDF
Welcome to python
PDF
Implementing Software Machines in C and Go
PDF
Implementing virtual machines in go & c 2018 redux
PDF
Go a crash course
PDF
Playing 44CON CTF for fun and profit
PDF
Are we ready to Go?
PPT
Python 101 language features and functional programming
PDF
Implementing Software Machines in Go and C
PDF
Recognize Godzilla
PDF
When RV Meets CEP (RV 2016 Tutorial)
PDF
1 seaborn introduction
PPTX
Python for Scientists
PDF
Metarhia KievJS 22-Feb-2018
PDF
A, B, C. 1, 2, 3. Iterables you and me - Willian Martins (ebay)
PDF
Docopt
PPTX
โปรแกรมย่อยและฟังชันก์มาตรฐาน
KEY
Parallel Computing in R
Introducción a Elixir
The secrets of inverse brogramming
JavaScript @ CTK
Welcome to python
Implementing Software Machines in C and Go
Implementing virtual machines in go & c 2018 redux
Go a crash course
Playing 44CON CTF for fun and profit
Are we ready to Go?
Python 101 language features and functional programming
Implementing Software Machines in Go and C
Recognize Godzilla
When RV Meets CEP (RV 2016 Tutorial)
1 seaborn introduction
Python for Scientists
Metarhia KievJS 22-Feb-2018
A, B, C. 1, 2, 3. Iterables you and me - Willian Martins (ebay)
Docopt
โปรแกรมย่อยและฟังชันก์มาตรฐาน
Parallel Computing in R
Ad

Similar to Exploring slides (20)

PDF
Python Cheat Sheet
PDF
intro_to_python_20150825
PDF
Introduction to python cheat sheet for all
PDF
Python Part 1
PPTX
Python programming
PDF
Python_Cheat_Sheet_Keywords_1664634397.pdf
PDF
Python_Cheat_Sheet_Keywords_1664634397.pdf
ODP
PDF
Mementopython3 english
PDF
Python_ 3 CheatSheet
PDF
Python for scientific computing
PDF
python.pdf
PDF
Python 101 1
PPT
Introduction to coding using Python
PDF
PDF
Mementopython3 english
PPT
Python tutorial
PPT
Profiling and optimization
PDF
PPT
Introduction to Python Programming.ppt
Python Cheat Sheet
intro_to_python_20150825
Introduction to python cheat sheet for all
Python Part 1
Python programming
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
Mementopython3 english
Python_ 3 CheatSheet
Python for scientific computing
python.pdf
Python 101 1
Introduction to coding using Python
Mementopython3 english
Python tutorial
Profiling and optimization
Introduction to Python Programming.ppt
Ad

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Electronic commerce courselecture one. Pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Modernizing your data center with Dell and AMD
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
MYSQL Presentation for SQL database connectivity
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation theory and applications.pdf
Understanding_Digital_Forensics_Presentation.pptx
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Electronic commerce courselecture one. Pdf
Encapsulation_ Review paper, used for researhc scholars
Modernizing your data center with Dell and AMD
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
MYSQL Presentation for SQL database connectivity
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Big Data Technologies - Introduction.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Per capita expenditure prediction using model stacking based on satellite ima...
The AUB Centre for AI in Media Proposal.docx
Spectral efficient network and resource selection model in 5G networks
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation theory and applications.pdf

Exploring slides