SlideShare a Scribd company logo
MAKING BEAUTIFUL
PYTHON CODE
Or Readability Counts
BY

JAIME BUELTA
Developers like
Python because code
is beautiful
Almost no one will appreciate it
(At least until there’s a problem)
BOSS DOES NOT CARE
worried about costs and stuff
CUSTOMERS DON’T CARE
worried about functionality and stuff
but your team will care
most of the time,
the effort difference
between good code
and bad code is small
MY
DREAM
Make beautiful Python code
Make beautiful Python code
Make beautiful Python code
Make beautiful Python code
Make beautiful Python code
CODE IS ALWAYS CHANGING
CODE NEEDS TO WORK
“There's a difference between
getting your hands dirty and
being dirty.”
DS Justin Ripley

(yes, the picture is not
from Luther, but from
The Good Cop)
WHAT MAKES BEAUTIFUL
CODE?
EASY TO UNDERSTAND
(THEREFORE, EASY TO
CHANGE)
NOT THE OTHER WAY
AROUND
OVERDESIGN
GOOD CODE
FITS IN YOUR HEAD
BAD
CODE...
Make beautiful Python code
System

Module
Function
Show how good you are HERE

Not here
Make beautiful Python code
Make beautiful Python code
LOCALITY
CONSISTENCY
VERBOSITY
LOCALITY
Keep related stuff together

CONSISTENCY
VERBOSITY
LOCALITY

CONSISTENCY
Use patterns

VERBOSITY
LOCALITY
CONSISTENCY

VERBOSITY

When in doubt, explain
in other words...

Be as OBVIOUS as possible
LOCALITY
MSG_TEMPLATE = ‘Template {date}: {msg}’
MORE_CONSTANTS...
....

def log_msg(message):
formatted_msg = MSG_TEMPLATE.format(
date=utcnow(),
msg=this_message
)
print formatted_msg
def log_msg(message):
MSG_TEMPLATE = ‘{date}: {msg}’
formatted_msg = MSG_TEMPLATE.format(
date=utcnow(),
msg=message
)
print formatted_msg
MSG_TEMPLATE = ‘{date}: {msg}’
def log_msg(message):
formatted_msg = TEMPLATE.format(
date=utcnow(),
msg=message
)
print formatted_msg
Related classes on
the same module

Keep files short

Function used only in
a module not in
independent module
Avoid
boilerplate

Separated
business logic
CONSISTENCY
Humans are amazing
recognizing patterns

(so good we tend to see too many)
PEP8 IS THE MOST
IMPORTANT PATTERN
COLLECTION OUT THERE
Abstract common operations
No getters and setters

List comprehensions
Decorators and with
statement
No private methods
I LOVE MY BRICK!

syndrome
VERBOSITY
data = "+RESP:GTTRI,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,"
"%s,%s,%s,%s,%s,%s,%s0" % (imei, number,
reserved_1, reserved_2,
gps_fix, speed,
heading, altitude, gps_accuracy,
longitude, latitude, send_time,
mcc, mnc, lac, cellid, ta,
count_num, ver)
data = (‘+RESP:GTTRI,{imei},{number},{reserved_1},’
‘{reserved_2},{gps_fix},{speed},{heading},’
‘{altitude},{gps_accuracy},{longitude},’
‘{latitude},{send_time}, {mmc}, {lac}, {cellid}’
‘{ta},{count_num}, {version}’).format(
imei=imei,
number=number,
reserved_1=reserved_1,
reserved_2=reserved_2,
gps_fix=gps_fix,
speed=speed,
heading=heading,
altitude=altitude,
gps_accuracy=gps_accuracy,
longitude=longitude,
latitude=latitude,
send_time=send_time,
mcc=mcc,
mnc=mnc,
lac=lac,
cellid=cellid,
ta=ta,
count_num=count_num,
version=ver,
)
WHEN IN DOUBT,
COMMENT
PUTTING ALL
TOGETHER
results = []
for row in query_results:
tag, value, updated = row
if value and updated > last_time:
TEMP = ‘tag: {0}, value: {1}
result = TEMP.format(tag,
value)
results.append(result)
def format_result(row):
tag, value, _ = row
TEMP = ‘tag: {0}, value: {1}’
return TEMP.format(tag, value)
def interesting(row):
_, value, updated = row
return value and updated > last_time
results = [format_result(row)
for row in query_results
if interesting(row)]
SMART IS THE ENEMY OF
READABILITY
HAVE A GOOD REASON TO
BE SMART
And comment accordingly
ONE DAY, SOMEONE WILL BE
SURPRISED WITH YOUR CODE
YOURSELF!!!
SO, REMEMBER
Make beautiful Python code
BE OBVIOUS
ASSUME YOU CAN’T FIT A LOT
OF CODE ON YOUR HEAD
BE SMART IN SYSTEMS, NOT
IN FUNCTIONS
LOCALITY
CONSISTENCY
VERBOSITY
Tell your buddies how much you
appreciate them writing simple code!
Make beautiful Python code

More Related Content

PDF
Using Buildout to Develop and Deploy Python Projects
ODP
Use of django at jolt online v3
PDF
Database madness with_mongoengine_and_sql_alchemy
PDF
Utopia Kindgoms scaling case: From 4 to 50K users
KEY
Use git the proper way
PDF
2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by...
PPTX
Python code profiling - Jackson Isaac
PDF
THE ZEN OF PYTHON
Using Buildout to Develop and Deploy Python Projects
Use of django at jolt online v3
Database madness with_mongoengine_and_sql_alchemy
Utopia Kindgoms scaling case: From 4 to 50K users
Use git the proper way
2013 Tips and Tricks Mashup, From ModelBuilder to Formal Python Code, Step-by...
Python code profiling - Jackson Isaac
THE ZEN OF PYTHON

Viewers also liked (6)

KEY
Do more than one thing at the same time, the Python way
PDF
Zen and the Art of Python
PPTX
Mongo db
PPT
Politics And Governance With Phil. Constitution boa
PDF
Visual Thinking Games
PPT
Visual basic ppt for tutorials computer
Do more than one thing at the same time, the Python way
Zen and the Art of Python
Mongo db
Politics And Governance With Phil. Constitution boa
Visual Thinking Games
Visual basic ppt for tutorials computer
Ad

Similar to Make beautiful Python code (20)

PDF
You can't spell "monitoring" without "monoid"
PDF
Basics of Programming - A Review Guide
PDF
Tips And Tricks For Bioinformatics Software Engineering
PPTX
Data oriented design and c++
PPTX
The R of War
PDF
Mastering Customer Segmentation with LLM.pdf
PDF
Beyond the code. Complexity - 2025.05 - SwiftCraft
PPTX
Intelligent Ruby + Machine Learning
PDF
PyDX Presentation about Python, GeoData and Maps
PPTX
2020 04 10 Catch IT - Getting started with ML.Net
PDF
A Small Talk on Getting Big
PPT
notes as .ppt
PDF
StatsD DevOps Boulder 7/20/15
PDF
Prophet - Beijing Perl Workshop
PDF
Beyond Breakpoints: A Tour of Dynamic Analysis
PPT
Screencasting and Presenting for Engineers
PDF
Maintainability SFJS Sept 4 2014
PPTX
L1-Introduction for Computer Science.pptx
PPT
Pragmatics of Declarative Ajax
You can't spell "monitoring" without "monoid"
Basics of Programming - A Review Guide
Tips And Tricks For Bioinformatics Software Engineering
Data oriented design and c++
The R of War
Mastering Customer Segmentation with LLM.pdf
Beyond the code. Complexity - 2025.05 - SwiftCraft
Intelligent Ruby + Machine Learning
PyDX Presentation about Python, GeoData and Maps
2020 04 10 Catch IT - Getting started with ML.Net
A Small Talk on Getting Big
notes as .ppt
StatsD DevOps Boulder 7/20/15
Prophet - Beijing Perl Workshop
Beyond Breakpoints: A Tour of Dynamic Analysis
Screencasting and Presenting for Engineers
Maintainability SFJS Sept 4 2014
L1-Introduction for Computer Science.pptx
Pragmatics of Declarative Ajax
Ad

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Spectroscopy.pptx food analysis technology
PDF
Machine learning based COVID-19 study performance prediction
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Empathic Computing: Creating Shared Understanding
PPT
Teaching material agriculture food technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Spectroscopy.pptx food analysis technology
Machine learning based COVID-19 study performance prediction
“AI and Expert System Decision Support & Business Intelligence Systems”
Empathic Computing: Creating Shared Understanding
Teaching material agriculture food technology
Digital-Transformation-Roadmap-for-Companies.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
MIND Revenue Release Quarter 2 2025 Press Release
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Programs and apps: productivity, graphics, security and other tools
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
Per capita expenditure prediction using model stacking based on satellite ima...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The AUB Centre for AI in Media Proposal.docx
Understanding_Digital_Forensics_Presentation.pptx
Electronic commerce courselecture one. Pdf

Make beautiful Python code