Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Pycon_Africa.pdf
Search
Prajjwal Nijhara
August 06, 2020
Programming
98
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Pycon_Africa.pdf
Where and How to use Asserts in your Python code!
Best Practices!
Prajjwal Nijhara
August 06, 2020
More Decks by Prajjwal Nijhara
See All by Prajjwal Nijhara
Raft_Consensus_Algorithm_Prajjwal.pdf
pnijhara
0
35
Setting up your Python environment for ML
pnijhara
0
52
Other Decks in Programming
See All in Programming
contenteditable と日本語入力に向き合う
colorful12
0
200
Herb in Rails 8.2: Your ERB views, now HTML-aware @ Kaigi on Rails 2026, Tokyo, Japan
marcoroth
2
220
カツオ、ご期待ください
suneo3476
0
190
AmebaブログはKMPから卒業します
kmars0605
0
190
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
1k
Railsバージョンを4倍にした話
optim
0
350
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
220
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.2k
APNsからLive Activityを開始する話
yumnumm
0
170
The Rails Doctrine Decade
koic
2
590
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.6k
Featured
See All Featured
The Spectacular Lies of Maps
axbom
PRO
1
1.1k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
410
Facilitating Awesome Meetings
lara
57
7.2k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.8k
What the history of the web can teach us about the future of AI
inesmontani
PRO
2
740
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.7k
So, you think you're a good person
axbom
PRO
2
2.2k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
360
Building Applications with DynamoDB
mza
96
7.2k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
Building the Perfect Custom Keyboard
takai
2
910
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
Transcript
Where and How to use Asserts in your python code
~Prajjwal Nijhara Twitter: @NijharaPrajjwal Github: @pnijhara Blogs: https://pnijhara.github.io
The assert statement exists in almost every programming language. It
helps detect problems early in your program, where the cause is clear, rather than later as a side-effect of some other operation.
How Asserts work?
Assert Equivalent to
Example
Output:
Then where is the problem?
Python’s -O (optimize) flag
During this, these basic optimizations are done: • All the
assert statements are removed • All docstrings are removed (when -OO is selected) • Value of the __debug__ built-in variable is set to False What goes down?
Let us test the above code with -O flag
What can go wrong? If assert statements are used for
user-facing validation in production code, the block won’t be executed at all — potentially opening up a security vulnerability.
Look how syrusakbary validates mail
Fix! It is recommended to use Asserts only in tests.
Issue in Mopidy
Fix!
~Prajjwal Nijhara Twitter: @NijharaPrajjwal Github: @pnijhara Blogs: https://pnijhara.github.io Thank You!
References - https://discuss.deepsource.io/t/using-assert-outside-tests/79 - https://deepsource.io/blog/python-common-mistakes/ - https://github.com/syrusakbary/validate_email/blob/master/validate_email.py#L127 - https://github.com/mopidy/mopidy/issues/1903 -
https://github.com/mopidy/mopidy/pull/1904/files - https://deepsource.io