python: len(x) vs x.__len__() (beginner - intermediate) anthony explains #131

preview_player
Показать описание
today I talk about the difference between len(x) and x.__len__() (as well as other similar functions!)

==========

I won't ask for subscriptions / likes / comments in videos but it really helps the channel. If you have any suggestions or things you'd like to see please comment below!
Рекомендации по теме
Комментарии
Автор

For anyone interested: For built-in types the CPython implementation of len() returns the value of the ob_size field in the PyVarObject C struct that represents any variable-sized built-in object in memory. Much faster than calling a method. (Fluent Python Book)

alizaini
Автор

Often when doing some data transformation pipeline it's easier to call __dunder__ methods than use functions.
Because you can keep typing forward w/o need to move to begining of line and do arrows/parentheses stuff.

Like

VS:

x * BOL iter( EOL ) * BOL next( EOL ) * BOL len( EOL )

not for production but for jupyter data exploration

tandavme
Автор

Tangentially, I keep seeing people more often use __dict__ instead of vars()

xan
Автор

The shirt... shouldn't it be: "test_allTheThings()" ? .. Maybe even better: "test_all_the_things()"?
Imho, thats how pytest would discover it. But I'd be happy to be corrected. In any case, we should have a video with that test implemented :D

JakubYTb
Автор

Good. I struggle with import paths on windows, that I solve by adding a path to PYTHONPATH, is there a way to avoid doing so? Thanks

optimiserlenergie
Автор

Why do Generators require multiple returns in mypy?

arclight