Тёмный

Custom Format Specifiers for Python Classes 

NeuralNine
Подписаться 353 тыс.
Просмотров 4,6 тыс.
50% 1

Наука

Опубликовано:

 

17 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 12   
@allan_c
@allan_c 9 месяцев назад
Thanks! I got some ideas to improve my module caled eletrica, that intends to be a simple way to calculate values in operations between rectangular (impedances) and polar (phasors)
@redcraft612
@redcraft612 9 месяцев назад
7:55 Using a format specifier inside a formatting function, peak humour!
@philtoa334
@philtoa334 9 месяцев назад
Thx_.
@JLSXMK8
@JLSXMK8 9 месяцев назад
***UPDATE: I cannot use PyCharm for any of my upcoming Python practice projects via these tutorials. I tried upgrading my PyCharm version a few weeks ago, and ended up losing it forever due to the new installation containing a virus. I will swap to using Visual Studio Code for the foreseeable future.*** This is so cool! So now, you can have a number class that, say, can have a hexadecimal, binary, octal, decimal, perhaps base64 representation, etc. and override the "def __format__(self, format_spec: str)" method to return the corresponding format for each of those options. That's very handy!!!
@IonizedComa
@IonizedComa 9 месяцев назад
If you have windows and want to get used of using Linux I suggest getting WSL, whether you're coding C, python, JavaScript etc managing packages is way easier with a few commands And you don't ever have to worry about viruses when using a package manager
@thomasgoodwin2648
@thomasgoodwin2648 9 месяцев назад
Dude! Working sick? Totally beast, but you really need to look after your health! Take care of yourself Brah! You're still young so it's easy now, but every time you do this makes the subsequent ones that much harder (voice of experience). As to format specifiers, 🤔,,, great ... more boilerplate code to have to knock out for every class. 🤕 IDK. I can see this being good maybe for some specific use cases, like padding time for hourly coders or typing skills practice maybe? Just kidding, yeah I CAN see it being useful ... ish, and actually rather cool. If I were fleshing out an API for publication as an example. I am probably in a minority here, but as a casual coder with responsibility for the entire project, I find myself looking for ways to decrease my typing. Slogging through vast wastelands of boilerplate, whether it's getters and setters in Java, or __str__, __repr__, if __name__ == "__main__" ... blah blah in Python, on and on (kinda like my comments😉) , really sux having to write the same code, over and over, every time. Takes all the fun and muse out of coding imo. As an observation of a 45+ year coder, I can say that no language is immune (though some clearly better than others), and that it generally gets worse with time as a language matures. Hoping you're feeling better soon. ✌and ❤ 🖖😎👍
@Snakyy1
@Snakyy1 9 месяцев назад
Why not just use class.method() instead? What does this have over regular class functions?
@phsopher
@phsopher 9 месяцев назад
For the same reason f-strings exist in the first place. It's just syntactic sugar for calling a method which returns a formatted string. You can always use a method instead if you prefer. For example, datetime implements both. You can either call d.strftime("Today is %A, %B %-d, %Y") or do f"{d:Today is %A, %B %-d, %Y}" or f"Today is {d:%A, %B %-d, %Y}".
@archardor3392
@archardor3392 9 месяцев назад
Ok, but why? Why would you even use that? How is that any better than just returning the polar coordinates from a normal method and not having it tied to formatting?
@samrijijkot
@samrijijkot 9 месяцев назад
You can add stuff that only makes sense when printing and not pollute your "normal methods" with it, nor do you have to add extra methods for printing in verious representations. Encapsulation, cleaner interface
@JLSXMK8
@JLSXMK8 9 месяцев назад
@@samrijijkot i.e., then __str__() could call __format__() implicitly if any format specifiers were to be passed in. The parser for the CPython compiler would be most likely the thing to detect this.
@ButchCassidyAndSundanceKid
@ButchCassidyAndSundanceKid 9 месяцев назад
1st.
Далее
Garbage Collection in Python: Speed Up Your Code
16:41
O-Zone - Numa Numa yei на русском!🤓
00:56
Просмотров 255 тыс.
Minecraft Pizza Mods
00:18
Просмотров 2,2 млн
Advanced Serialization with dill in Python
16:32
Просмотров 6 тыс.
Makefiles Make Your Life Easier
12:05
Просмотров 88 тыс.
How To Use Multiprocessing In Python
5:00
Просмотров 503
Python Decorators in 15 Minutes
15:14
Просмотров 431 тыс.
Python Sockets Simply Explained
39:33
Просмотров 157 тыс.
5 Good Python Habits
17:35
Просмотров 432 тыс.
Argument Parsing with argparse in Python
11:27
Просмотров 13 тыс.
Functions vs Classes: When to Use Which and Why?
10:49
Просмотров 146 тыс.
What Are * and / Parameters in Python Functions?
5:44
Телефон-електрошокер
0:43
Просмотров 428 тыс.