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
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
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
180
AtCoder Conference 2025
shindannin
0
1.1k
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
300
AIフル活用時代だからこそ学んでおきたい働き方の心得
shinoyu
0
140
そのAIレビュー、レビューしてますか? / Are you reviewing those AI reviews?
rkaga
6
4.6k
今から始めるClaude Code超入門
448jp
8
8.9k
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
6.1k
CSC307 Lecture 04
javiergs
PRO
0
660
AWS re:Invent 2025参加 直前 Seattle-Tacoma Airport(SEA)におけるハードウェア紛失インシデントLT
tetutetu214
2
110
dchart: charts from deck markup
ajstarks
3
990
Best-Practices-for-Cortex-Analyst-and-AI-Agent
ryotaroikeda
1
110
2026年 エンジニアリング自己学習法
yumechi
0
140
Featured
See All Featured
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
93
ラッコキーワード サービス紹介資料
rakko
1
2.3M
Designing Experiences People Love
moore
144
24k
Making Projects Easy
brettharned
120
6.6k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.3k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
0
440
Joys of Absence: A Defence of Solitary Play
codingconduct
1
290
AI: The stuff that nobody shows you
jnunemaker
PRO
2
260
Embracing the Ebb and Flow
colly
88
5k
My Coaching Mixtape
mlcsv
0
48
Ethics towards AI in product and experience design
skipperchong
2
190
Abbi's Birthday
coloredviolet
1
4.8k
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