Mutable defaults: the WORST noob error in Python

preview_player
Показать описание
This is a really easy mistake to make in Python, and if that wasn't bad enough, when it starts causing problems, it's pretty difficult diagnose! In this video, we'll learn how the mistake can be made, and how to go about solving it.

0:00 - Intro
0:35 - Problem definition: functions
2:44 - Problem definition: classes
5:03 - Solving the problem by using `None` as a default
6:33 - Solving the problem using data classes
8:21 - Outro



~ Hotlinks ~

~ Setup ~

~ Affiliations ~

#python #coding #howto
Рекомендации по теме
Комментарии
Автор

It's just part of what makes Python a python. 💁‍♂

LinXnerd
Автор

A more elegant solution than the 'if lst is None: lst = []' clause in the function 'append_to_list' may be to use another Python idiom:
lst = lst or []
this is possible because the python 'or' operator does not return a boolean but rather the value of the side which is "truthy". Happy hacking! 🐍

hanspeterfake
Автор

Yo bro you did a series 4 years ago where you explained how to create a twitch bot. i would love to see an updated tutorial series with python 3.13

LordBratfisch
Автор

I like using it for caching with a default dictionary. It may be a noob way, but saves importing additional modules and using decorators or implementing cache yourself.

phsopher
Автор

Wasn't there a proposal to Python to get the safe behavior using := instead of = inside the function parameters?

revengerwizard
Автор

I 100% sure that your channel name came from capyberra 😁

rohithreddy
Автор

These are weird as hell haha, I did a short on this a few days ago! Very bizarre feature 😂

pythonwithjames
Автор

Boilerplate type hinting in python is just the worst... The function declaration is literally three times longer than it needs to be. Getting types confused is a myth - it just doesn't happen. Everything else does, but not that. This solves a non-existent problem and makes python ugly, harder to read and verbose.

Sorry, that was a bit negative but it depresses me that learners are being shown this.

davidmurphy
Автор

Hello friend, I've just started following your work, congratulations on the content, but if you allow me a suggestion, it's for you to create some more eye-catching thumbs, I believe you'll get more engagement

kyo
visit shbcf.ru