Python's magic methods

preview_player
Показать описание
Who wouldn't want to sprinkle a little magic onto their code? Well, with Python's magic methods, you can! You can even get some neat functionality out of it too.



If you enjoy my content, consider supporting me on Patreon or becoming a member!

If you need help with anything, feel free to join the Discord server:

I get a lot of people asking, so here's my Visual Studio Code setup!



If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so.

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

The front of the editor is to small for handheld devices.

zbigniewloboda
Автор

00:00 Intro
01:30 init
02:36 init_subclass
04:38 str
05:24 repr
06:29 int, float
06:49 hash
07:38 operators (add, ...)
11:00 i-operators (iadd, ...)
12:15 equality operators (eq, ne)
13:40 comparison operators (le, lt, ge, gt)
15:09 contains (in operator)
16:24 getitem, getattr (index operator)

cn-ml
Автор

The font is way to small for my phone.

JorgeEscobarMX
Автор

There's an ORM package called SQLModel created by the author of FastAPI, which is built on top of SQL Alchemy and Pydantic, utilized the __init_subclass__ dunder method to distinguish the defined class should be a database table or just or a normal Pydantic model.

Alan
Автор

Hey, great video showcasing those methods! I was wondering, how did you manage to get that beautiful, clean terminal? Mine shows unnecessary paths and details and a bunch of other ugly stuff before actually displaying the output of my program. Perhaps you could make a tutorial on how to clean up the terminal?

doritorick
Автор

13:19
I think it'd be better to raise a TypeError when trying to compare the object with an object of a different type:
from typing import Self
def __eq__(self, other: Self) -> bool:
if not isinstance(other, self.__class__): raise TypeError(f'The equality operator is not defined between {self.__class__} and {other.__class__} ')
return ... # normal logic between two instances of Self
This wouldn't let such comparisons (which are almost surely errors) run quietly.

airatvaliullin