5 Useful Dunder Methods In Python

preview_player
Показать описание
Today we will be learning about 5 useful dunder methods that we can use in Python.

▶ Become job-ready with Python:

▶ Follow me on Instagram:

00:00 Learning Python made simple
00:05 Intro
00:14 __eq__
03:49 __format__
05:42 __or__
08:41 __repr__
11:24 __getitem__
15:51 What are your thoughts?
Рекомендации по теме
Комментарии
Автор

One minor point: to make the search case-insensitive, you should really lower the input as well.

xinaesthetic
Автор

F-strings can also show the repr of an object using the !r specifier: f"{item!r}"

Bwanshoom
Автор

one common pythonic thing is, repr(instance) should return a string so that:

>>>instance == eval(repr(instance))


is True, so something _like_:

return f"{type(self).___name__}(}" + ", ".join(f'{k}={v}' for k, v in self.___dict___.items()) + ")"

Also: note that these dunder methods are _strongly typed_ and MUST return a str.

You can also use the "dunder module" static class attribute to assist.

DrDeuteron
Автор

Your content is one of the best, I would never have imagined that __str__ was different than __repr__

poixdro
Автор

Man! I had no idea Python could do all of this. So glad the algo picked this. Thanks for the info!

rodrigodanielvittoriali
Автор

Good video, but Pyright suggest you to use `self.__class__` when instating the same class instead of using `ClassName(...)`.
So in the `__or__` method you should instead return `self.__class__(...)`, and basically in every method that returns a new instance of Self. Hope it helps!

developer_anonymous
Автор

Two and a half kilo Apple! I did dream about such a thing when I was a kid, familiar with scrumping.

SusanAmberBruce
Автор

Wow I have been sitting on a use-case for union, intersection and subtraction methods for the past year but never knew the syntax could be so nice with these dunder methods (__or__ and friends). Thank you!

DownThereForDancing
Автор

Personally, I would require that the name representation was always lowercase anyway, even on initialization. As for fun with operators, I like to overload / on strings in languages that don't already do so to act as the split operation. Say you've got a string that's s = "foo, bar, baz, luhrmann"; then a = s / ', '; would yield an array of strings containing ["foo", "bar", "baz", "luhrmann"].

anon_y_mousse
Автор

Thank you, very intuitive and precise presentation!

pavfrang
Автор

Another cool video, thanks! Hope to see more ___dunders__ :)

corckpp
Автор

Man this is crazy helpful, this is some advanced stuff

workingguy
Автор

Great video, thanks
One extra thing which I learned was that you can do filter with list comprehension. Until now I did filter + lambda (which basically was shooting myself in the leg because of whacky annotations).Thanks again

Mefodii
Автор

I'm just thankful I finally know what these methods are called. Every few months or so, I come up with an idea with classes, and I need to scour the internet 15min for the python documentation on all these methods.

VoxelPrismatic
Автор

Awesome video! Your explanation is really good! Thanks a lot!

rodelias
Автор

Like this and I agree with @xinaesthetic about the input should be also be converted. But you should use 'casefold()' instead of 'lower()', in these examples it might not matter but it should always be used when a comparison is made. Keep up with the videos.

kristerl
Автор

you do it so useful for all of us. I know what you are one of the best python developer.

shaxzodbek
Автор

From my Java days, one rule I’ve always followed is: “Always define toString!” In Python, it’s: “Always define dunder str and repr!” It should be one of the first things you do when you write a class. (FWIW, I typically use dunder repr to return a JSON-like string, but that’s just me.)

TheWyrdSmythe
Автор

Thanks alot for the quality content you provide! One question: what is the extension that shows you classes and methods usages throughout code? When coding rust that thing is auto enabled (thanks to the compiler features I guess)… thanks in advance :)

stefanocardarelli
Автор

If I do order with subscriptions, this channel stays. Great kind of practical tutorial 🔥

krzysiekkrzysiek