• miohtama an hour ago

    Reasons to use dataclass(slots=True) instead of TypedDict

    - Faster attribute access: your code is faster

    - Slotted classes take less RAM, less L1 cache pressure, your code is faster

    - Wrist friendly to with .foobar instead of ["foobar"]

    - Runtime error if you misspell an attribute name

    • fovc an hour ago

      It’s Python. Does L1 matter at all? I assume anything you’re accessing is behind a few pointers and __dict__ accesses anyway.

      For me it’s mostly about .attribute being more in line with the rest of the language. Kwargs aside, I find overuse of dicts to clunky in Python

      • gshulegaard an hour ago

        Nope, __slots__ exist explicitly as an alternative to __dict__:

        https://wiki.python.org/moin/UsingSlots

        Whether or not the performance matters...well that's somewhat subjective since Python has a fairly high performance floor which makes performance concerns a bit of a, "Why are you doing it in Python?" question rather than a, "How do I do this faster in Python?" most of the time. That said it _is_ more memory efficient and faster on attribute lookup.

        https://medium.com/@stephenjayakar/a-quick-dive-into-pythons...

        Anecdotally, I have used Slotted Objects to buy performance headroom before to delay/postpone a component rewrite.

      • Spivak 6 minutes ago

        Better than both use Pydantic. You'll never want to use anything else ever again. It's truly transformative in how you write code. Full type hinting support as well as strong verification that your data actually conforms to the types you set. Full recursive parsing of types arbitrarily nested and can parse tagged and untagged unions.

      • jjtheblunt 2 hours ago

        "thank you think" in a title is overtly condescending...you know your audience and their educational deficiencies?

        "than i thought" would be compelling to read.

        • crazygringo 2 hours ago

          Yup, using "you" in headlines is a pattern that needs to die.

          I get that it's attention-grabbing, but it's because it's rude.

          You don't know anything about me. You don't know what I think or what I already know or what I won't believe.

          I know it's not a big deal in the grand scheme of things, but it's just one of those little aggravating things that makes life just a little bit worse each time you come across them.

          • robswc 5 minutes ago

            The one I hate the most is:

            "X thing did Y, here's why Z"

            Where X and Y can range from meaningless to national security but Z always remains an opinion (usually uninformed too).

            Just search "here's why that's bad/good" on google news and there's so much "slop."

          • tptacek an hour ago

            This is the kind of copyediting advice ChatGPT gives. I think everyone gets that the author doesn't know what you, in particular, think about TypedDicts. Read things more charitably; this is not a good use of time to discuss.

            • sqeaky 22 minutes ago

              100%! If people are dumb enough to think I am dumb, then they generally aren't saying anything worth listening to.

              • lopatin 2 hours ago

                It's a qualifier. In a sense, they do know their audience fairly well because someone who clicks on that link is intrigued and feel they have more to learn about it. Anyone who is pretty knowledgeable about the subject will go "pff .. yeah right" and not even click on it.

                That said, it does annoy me too.

                Also what annoys me is that I constantly try to play devils advocate for things like this even if I don't always agree with the conclusion of the advocate.

              • benmmurphy 2 hours ago

                  <Option<Option<subscription>>