Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Pycon_Africa.pdf
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Prajjwal Nijhara
August 06, 2020
Programming
1
88
Pycon_Africa.pdf
Where and How to use Asserts in your Python code!
Best Practices!
Prajjwal Nijhara
August 06, 2020
Tweet
Share
More Decks by Prajjwal Nijhara
See All by Prajjwal Nijhara
Raft_Consensus_Algorithm_Prajjwal.pdf
pnijhara
0
22
Setting up your Python environment for ML
pnijhara
0
39
Other Decks in Programming
See All in Programming
組織で育むオブザーバビリティ
ryota_hnk
0
180
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
200
AIによる高速開発をどう制御するか? ガードレール設置で開発速度と品質を両立させたチームの事例
tonkotsuboy_com
7
2.4k
Honoを使ったリモートMCPサーバでAIツールとの連携を加速させる!
tosuri13
1
180
余白を設計しフロントエンド開発を 加速させる
tsukuha
7
2.1k
Fluid Templating in TYPO3 14
s2b
0
130
20260127_試行錯誤の結晶を1冊に。著者が解説 先輩データサイエンティストからの指南書 / author's_commentary_ds_instructions_guide
nash_efp
1
980
Claude Codeと2つの巻き戻し戦略 / Two Rewind Strategies with Claude Code
fruitriin
0
130
AI Schema Enrichment for your Oracle AI Database
thatjeffsmith
0
300
CSC307 Lecture 08
javiergs
PRO
0
670
Oxlint JS plugins
kazupon
1
970
OCaml 5でモダンな並列プログラミングを Enjoyしよう!
haochenx
0
140
Featured
See All Featured
The Spectacular Lies of Maps
axbom
PRO
1
520
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
76
Six Lessons from altMBA
skipperchong
29
4.1k
The Curse of the Amulet
leimatthew05
1
8.6k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.6k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
200
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
1.9k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
430
Mobile First: as difficult as doing things right
swwweet
225
10k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
270
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
1
1.3k
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