Python Experts Love Using THIS Magic Method

preview_player
Показать описание
Python experts love using this magic method. #code #programming #python
Рекомендации по теме
Комментарии
Автор

This confuses the code. len() should be a property of the datatype, not a characteristic of the book. The length of the book should be the pages field or other internal field. An array has a length, a list, a set, a dict, most data structures. if a book has a length/size, should be defined using specific fields and specific getters/setters.

elatedbento
Автор

Well... that's some way to get yourself fired.

papercuttzi
Автор

@Indently, I really enjoy your channel and I think it is commendable to see you demonstrate the ability to define __len__ and how it can be useful. Not everyone knows that. However, the example of Book was a poor choice for the case. Users should use the pages field and specific getters/setters, if possible.

elatedbento
Автор

Confusing he wants the length of the book or gets the pages? This question is confusing too.

bene.the.noobster
Автор

Well, this actually could be useful if used consistently. However that depends on overall usage of this class and programmer needs to know that defining dunder methods has consequences. For example if we define __len__ like that, we'll get `bool(Book("Something", 0)) == False`. That could easily backfire if behaviour is not intentional.
Usually defining __len__ makes more sense for custom collections together with __getitem__ and other cases like that.

evlezzz
Автор

magic methods are awesome, try operator overloading its really powerful, its simplify the code base by abstracting the complex logic between 2 or more objects to simply an operator.

paljain
Автор

__length__() and len() functions are ment to be used for array or array-like structures where the object holds multiple elements. for example a linked list, or a binary tree structure. Never ever use it like in this exapmle. As someone in comments said, it is a great way to get yourself fired.

furkanunsal
Автор

But what if there's an array of books with 5 elements? What will the Len function return?

tarunrathitra
Автор

What is the real life advantage of doing this over using the book pages variable?

rain-er
Автор

what is the extension or option that makes vs code show usages of the class?

aralacikalin
Автор

That is not a good example, you shouldnt use len in this example, a good example is getting the length of a linked list, where you actually use less code

hiperion_
Автор

Cool vid, but maybe there should’ve been a different example, because this is definitely confusing😅

wm
Автор

Instead of passing book into Len(), can we also do something like book().length

thedelanyo
Автор

It doesn’t seem logical to return an array in a length method. I know, this is by design, but I would expect __len__ to return an integer.

romaklimenko
Автор

Wait what...???

Len(book)
*Slaps myself*

Am I even watching a real tutorial here?

LittlesProductReviews
Автор

Heh, as a writer of code and prose, I can't fault this on its technical aspect, but I would point out that a book's length in pages will vary between editions. Wordcount too.

So having len(Book) return a custom object that extends Int but has an array of editions and each ones' length? 🤔

ConstantlyDamaged
Автор

Please don't name your variables and attributes like he did with 'pages' it's so confusing and more in python because of no typing use. If your variable is intended to count, then just call it like 'page_count' and not 'page_number' bacause it's still confusing

bobbybibbou
Автор

Python is the best language ever to exist.

ShivanshKaran
Автор

This is just getting the pages I think, not the length of the book, my opinion though and it can be easily gotten by just print(book.pages). The additional method is confusing lol

akilahafaf
Автор

print(book.pages)😂😂when you want to know pages only

Dpk_YT