Custom Format Specifiers for Python Classes

Поділитися
Вставка
  • Опубліковано 21 гру 2024

КОМЕНТАРІ • 12

  • @allan_c
    @allan_c Рік тому +1

    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 Рік тому

    7:55 Using a format specifier inside a formatting function, peak humour!

  • @Snakyy1
    @Snakyy1 Рік тому +1

    Why not just use class.method() instead? What does this have over regular class functions?

    • @phsopher
      @phsopher Рік тому +1

      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}".

  • @JLSXMK8
    @JLSXMK8 Рік тому

    ***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 Рік тому +1

      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 Рік тому +1

    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 ❤
    🖖😎👍

  • @philtoa334
    @philtoa334 Рік тому

    Thx_.

  • @archardor3392
    @archardor3392 Рік тому +4

    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 Рік тому +2

      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 Рік тому

      @@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 Рік тому +1

    1st.