python parameter defaults are (mutable) globals (intermediate) anthony explains #364

python parameter defaults are (mutable) globals (intermediate) anthony explains #364

anthonywritescode

3 года назад

1,113 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@Khushpich
@Khushpich - 29.11.2021 20:23

Great video Anthony. This is the most unintuitive thing in python for me. Any reason why it behaves this way?

Ответить
@ltn100
@ltn100 - 29.11.2021 20:55

Nice! I was surprised that you hadn't covered this already, but definitely a good tip to know.
Regarding the check for the None value of x, I've gotten into the habit of writing "x = x or []" (I guess I'm lazy and only want to type one line!). I'd be interested to know if you thought this was good or bad practice?

Ответить
@yarontaub
@yarontaub - 29.11.2021 21:35

Hi! Love your videos. But I usually watch them on my phone, so everything is a bit small. Maybe you can enlarge the font? Thanks :-)

Ответить
@JohnZakaria
@JohnZakaria - 30.11.2021 01:35

Instead of writing
If x is None
x=x or (default) will do the trick
Because boolean operators don't return bools

Ответить
@mfeickert07
@mfeickert07 - 30.11.2021 22:21

This was great. I knew to avoid mutable defaults but genuinely didn't know that `__defaults__` existed. Thanks!

Ответить