Knowing this can save you HOURS of debugging! (5 Python Pitfalls)

  Рет қаралды 46,692

Patrick Loeber

Patrick Loeber

Күн бұрын

Being aware of these 5 Python Pitfalls can save you hours of debugging effort.
Credits: github.com/satwikkansal/wtfpy...
Get my Free NumPy Handbook:
www.python-engineer.com/numpy...
✅ Write cleaner code with Sourcery, instant refactoring suggestions in VS Code & PyCharm: sourcery.ai/?... *
⭐ Join Our Discord : / discord
📓 ML Notebooks available on Patreon:
/ patrickloeber
If you enjoyed this video, please subscribe to the channel:
▶️ : / @patloeber
~~~~~~~~~~~~~~~ CONNECT ~~~~~~~~~~~~~~~
🖥️ Website: www.python-engineer.com
🐦 Twitter - / patloeber
✉️ Newsletter - www.python-engineer.com/newsl...
📸 Instagram - / patloeber
🦾 Discord: / discord
▶️ Subscribe: / @patloeber
~~~~~~~~~~~~~~ SUPPORT ME ~~~~~~~~~~~~~~
🅿 Patreon - / patrickloeber
#Python
----------------------------------------------------------------------------------------------------------
* This is an affiliate link. By clicking on it you will not have any additional costs, instead you will support me and my project. Thank you so much for the support! 🙏

Пікірлер: 73
@johnjoyce
@johnjoyce 2 жыл бұрын
The single element tuple gotcha is half the reason I always always add the comma after the last element of every collection. It always expresses correctly and if aligned with each collection member or element on separate lines, then it is easier to reorder the collection in code and see deltas in diffs and help git better understand merges as it is so line oriented.
@__________________________6910
@__________________________6910 2 жыл бұрын
Thanks, "Happy Teachers Day" from INDIA
@b07x
@b07x 2 жыл бұрын
You're the same person who said that Loi should be the next James Bond.
@__________________________6910
@__________________________6910 2 жыл бұрын
​@@b07xyou remembered me
@__________________________6910
@__________________________6910 2 жыл бұрын
@@b07x YES
@coniferviveur3788
@coniferviveur3788 2 жыл бұрын
Thanks for that. Your explanations are very clear and your use of examples is excellent.
@imdadood5705
@imdadood5705 2 жыл бұрын
Once you learn the basics of python. This solidified the understanding further. Thank you for this!
@johnjoyce
@johnjoyce 2 жыл бұрын
Finally, most of these can be avoided by adopting the emerging more explicitly typed coding style using the typing module extensively with mypy and using explicit constructors to avoid ambiguity even when it is visual ambiguity. The fast and loose nature of many scripting languages has begun to wear thin and we are seeing the emergence of explicit typing style as scalable and reducing a class of runtime errors by preventing them. TypeScript is an example of this along with Hack and others.
@TylerThomas
@TylerThomas 2 жыл бұрын
you're right but I don't want you to be.
@13thk
@13thk 2 жыл бұрын
4:58 this tip applies to any built-in unnamed collection(list, tuple, str) For example (1,2,3) == ((1,2,3)) is true unless (1,2,3) == ((1,2,3),) which is false Or [2,3] == ([2,3]) which is true
@caiyu538
@caiyu538 2 жыл бұрын
great lectures. Really tricky in python. Thank you so much.
@raminmousivand4257
@raminmousivand4257 Жыл бұрын
Fantastic tutorials. Please keep going buddy 👌🙏
@pacersgo
@pacersgo 2 жыл бұрын
It is a very good video. I have written at least three of those bugs, that did take me quite some time to figure out what happened..
@patloeber
@patloeber 2 жыл бұрын
Haha we’ve all been there 😄
@JohnZakaria
@JohnZakaria 2 жыл бұрын
I have done all of these mistakes as I started doing python. Accuracy 100%
@castlecodersltd
@castlecodersltd Жыл бұрын
Really helpful, thank you ☺
@AR-fh2uh
@AR-fh2uh 2 жыл бұрын
I was there today. Working on a client server model. It wouldn't work for love or money. Turns out, after a complete refactor the client and server were using a different message header length. I was setting specifically in thier init statements.
@davidbeauchesne7399
@davidbeauchesne7399 2 жыл бұрын
thanks! good tips for the new python user.
@raiyan22
@raiyan22 2 жыл бұрын
learned sth new today !! Thanks
@vivekan97
@vivekan97 2 жыл бұрын
Very informative 👍👍👍 Looking for more
@patloeber
@patloeber 2 жыл бұрын
Glad you like it
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
1:54 Note that implicit string-literal concatenation happens at compile time, not run-time. It’s a feature copied from C, also present in C++ but for some reason omitted in Java and PHP. I like it, and I make use of it frequently. It’s more convenient for breaking long strings across multiple lines than triple-quoted strings. I use it to write things like this:
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
sys.stdout.write \ ( " ♯ nr scanned: %d, nr hashed: %d, time taken = %ds " "♯ delete: %d, touch: %d, move: %d " "♯ unindexed: %d " %
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
( nr_scanned, nr_hashed, round(finish_time - start_time), to_delete, to_touch, to_move, nr_unindexed, ) )
@personx9450
@personx9450 Жыл бұрын
Helpful 👏
@vinicus508
@vinicus508 2 жыл бұрын
4:25 when you showed this i had to pause to try to understand what in the world is that. And I asked myseld “why tf is it like this?”. But actually this makes a lot of sense, and its super useful now that I think of it
@victorpinasarnault9135
@victorpinasarnault9135 2 жыл бұрын
Thanks a lot
@rendomstranger8698
@rendomstranger8698 2 жыл бұрын
I wish I had seen this video 2 weeks ago. I spent about an hour debugging a program because of the whole 1 item tuple situation. Took me a while to figure out that it was the tuple that was causing problems and even longer before I discovered that the function it was a part of was returning a string instead of a tuple. Thinking about it further, I should probably be checking for type instead of length in my program and just leave the string as a string.
@urilou777
@urilou777 2 жыл бұрын
or just use C instead cuz python is bad
@rendomstranger8698
@rendomstranger8698 2 жыл бұрын
@@urilou777 Nah. I'll stick with Python for now. I'm trying to get into professional programming so no point in switching languages in the middle of a project that I'm planning to put on my CV.
@Tennisbull-match-statistics
@Tennisbull-match-statistics 2 жыл бұрын
What other functions are in place operations?
@Yotanido
@Yotanido 2 жыл бұрын
I knew about all of these and could correctly predict what each shown program was going to do. That said, append being an in-place operation and returning None has annoyed me a lot when I first started to use python. Coming from Haskell, mutating lists like that was just... weird. I still think it should return the list, even if it does change it in place, since it would allow you to chain calls.
@chaddaifouche536
@chaddaifouche536 2 жыл бұрын
Or at least it should return an error value or something that makes it clear your error was assigning the result of an in-place modification... Though your chain calls argument is good, as expected coming from Haskell where it's quite common to compose several functions.
@vandelayindustries2971
@vandelayindustries2971 2 жыл бұрын
Chaining it would be useful for people like us who know the difference between in-place and copying, but for beginners it could be confusing. They'll see a list being returned and assume it's a copy and so they'll treat it as such, introducing bugs all over their code. By returning None for in-place stuff and returning a value when it's a copy, there's never any doubt (the same way list.sort() returns None, but sorted() returns a copy).
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
If you don’t want to mutate the list, use the functional construct. Python does not claim‡ to be a pure-functional language, but at least it gives you the choice of functional constructs. ‡Is there really such a thing as a “pure-functional” language? Or only languages that _claim_ to be pure-functional?
@saurrav3801
@saurrav3801 2 жыл бұрын
🔥🔥🔥🔥Great job bro
@patloeber
@patloeber 2 жыл бұрын
thanks!
@saumodipdas3624
@saumodipdas3624 2 жыл бұрын
Really good video. Though I used to get correctly 4 of these but never paid attention to the errors otherwise. So thanks for the info. Keep it up 😀😀😀
@dominikwodarczyk6714
@dominikwodarczyk6714 2 жыл бұрын
Thanks
@johnjoyce
@johnjoyce 2 жыл бұрын
Yeah. That string concatenation “feature” is a misfeature in most contexts. Should be removed along with unparenthesized tuple because similarly hard to spot comma can cause syntactically valid but crazy bugs.
@laugedyret
@laugedyret 2 жыл бұрын
I love it!
@patloeber
@patloeber 2 жыл бұрын
Happy to hear that :)
@akshaysrivathsa227
@akshaysrivathsa227 2 жыл бұрын
Can u pls make a pytorch project like the snake game
@patloeber
@patloeber 2 жыл бұрын
thanks for the suggestion. which project would you like to see?
@akshaysrivathsa227
@akshaysrivathsa227 2 жыл бұрын
@@patloeber Pls make a general video so that we can implement it on any game we want.
@majkelpl8611
@majkelpl8611 2 жыл бұрын
I preferred your face rather than memes. Video as always, great.
@patloeber
@patloeber 2 жыл бұрын
thanks for the feedback :)
@JOHNSMITH-ve3rq
@JOHNSMITH-ve3rq Жыл бұрын
Appalled at this language. Though I also love it 😂
@maman14141414
@maman14141414 2 жыл бұрын
for the tuple example there is actualy a trick you can just write ("one",) wth a trailing comma to have the desired behavior
@patloeber
@patloeber 2 жыл бұрын
yep great tip!
@MrCmon113
@MrCmon113 2 жыл бұрын
I wrote a conditional that always returns true in my bachelor thesis. Hundreds of hours of GPU time wasted, because I only found the mistake after weeks.
@patloeber
@patloeber 2 жыл бұрын
oh noooo :D now this will never happen to you again
@siranjeevis1176
@siranjeevis1176 2 жыл бұрын
Post video more videos like this
@patloeber
@patloeber 2 жыл бұрын
I try :)
@evlezzz
@evlezzz 2 жыл бұрын
Explanation for conditional expression is wrong. It has nothing to do with consistency. It's caused by operations priority within expression. Since priority for comma operator is lower than for conditional operator, "(a, b) if x else c, d" is actually parsed as "((a, b) if x else c), d".
@lawrencedoliveiro9104
@lawrencedoliveiro9104 Жыл бұрын
The Python conditional-expression construct is a total abortion.
@evlezzz
@evlezzz Жыл бұрын
@@lawrencedoliveiro9104 It's not, if used properly.
@nort1793
@nort1793 2 жыл бұрын
I am sixth!!!
@Jxrdy
@Jxrdy 2 жыл бұрын
wtf python
@JOHNSMITH-ve3rq
@JOHNSMITH-ve3rq Жыл бұрын
My gosh that is such stupid behaviour by python. Ridiculous!!!
@chaosopher23
@chaosopher23 2 жыл бұрын
Those are not bugs, they are features.
@hossumquat
@hossumquat 2 жыл бұрын
It's only a feature if the code does what you want it to. If it doesn't, it's a bug. So context matters for making the distinction.
@chaosopher23
@chaosopher23 2 жыл бұрын
@@hossumquat What would you call it if you put it in & didn't tell anyone but your dev team, just, let them find it? For example, a room or two in a video game where executable code could be run on purpose. Not like that log4j hot mess!
@jesusmtz29
@jesusmtz29 2 жыл бұрын
i wonder who thought this was a good idea lol maybe ther'es a good reason
@paulabraham2550
@paulabraham2550 2 жыл бұрын
Good grief. The more I learn about python ...
@ktrivikram1311
@ktrivikram1311 2 жыл бұрын
Your project name is wtf python 🤣
@viddeshk8020
@viddeshk8020 11 ай бұрын
😂 seeing this in 2023 😅
@boem231
@boem231 2 жыл бұрын
This is exactly why I hate python
@michalbotor
@michalbotor 2 жыл бұрын
i have never expected to burst into laughter once someone had explained to me where the flaw in the code lies... i'm talking about you: `x, y = (1, 0) if condition else None, None`
@patloeber
@patloeber 2 жыл бұрын
Haha glad I could entertain you
10 Python Basics You Should Know!
10:08
Patrick Loeber
Рет қаралды 78 М.
31 ESSENTIAL Python String Methods
11:20
Patrick Loeber
Рет қаралды 81 М.
A pack of chips with a surprise 🤣😍❤️ #demariki
00:14
Demariki
Рет қаралды 41 МЛН
🍕Пиццерия FNAF в реальной жизни #shorts
00:41
Modern Python logging
21:32
mCoding
Рет қаралды 153 М.
How to create a great dev environment with Docker
29:55
Patrick Loeber
Рет қаралды 249 М.
6 Tips to write BETTER For Loops in Python
9:19
Patrick Loeber
Рет қаралды 246 М.
PyScript is officially here!🚀 Build web apps with Python & HTML
17:10
5 Good Python Habits
17:35
Indently
Рет қаралды 377 М.
Beautiful Terminal Styling in Python With Rich
13:17
Patrick Loeber
Рет қаралды 135 М.
7 Python Code Smells: Olfactory Offenses To Avoid At All Costs
22:10
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,5 МЛН
A pack of chips with a surprise 🤣😍❤️ #demariki
00:14
Demariki
Рет қаралды 41 МЛН