For example:

class FooBar:
    def __init__(self):
        self.a: int = None
        self.foo: str = None

Is this bad practice/go against PEP guidelines or is it fine?

  • logging_strict
    link
    fedilink
    arrow-up
    1
    ·
    11 days ago

    You are describing Sentinel. Recommend using attrs.NOTHING. Unfortunately, the static typing, NothingType, is in private API. Until the Python community can settle on the one Sentinel to rule them all, will stick with attrs cuz it’s usually already a dependency.

    You can come by my place anytime for beers and pizza. Bitching about static typing should be an Olympic sport. Or at least involve beer.

      • logging_strict
        link
        fedilink
        arrow-up
        1
        ·
        11 days ago

        i’m in love with typing_extensions. Know why? I’m not on the latest greatest Python version. But want to use the usable latest features. As package author/maintainer, upgrading to the latest and greatest is not a viable option.

        in py39, dataclasses lacked critical features that came out later. Especially for a fundamental structure like a dataclass, new features requiring a Python upgrade is a show stopper. On the level of a flaw. Either backport or in a separate package or i don’t want to touch it. That’s why use attrs and not dataclasses.dataclass

        And so should we all.

        There is always going to be some great features we want to use and then not being able to is not great UX. Every package that matters really must be external, except for Python features like free threading, sub-interpretors, etc.

        There are always many coding languages, unfortunately jumping from one to the next is a pipe dream. So really doesn’t matter what Haskell can do. Or how Rust devs all have 10 gfs.

        • solrize@lemmy.ml
          link
          fedilink
          arrow-up
          2
          ·
          9 days ago

          Before dataclasses I used namedtuple and that was good enough. attrs looked like an improvement over namedtuple but not enough of one to deal with the added complexity. If dataclasses have gotten interesting new features since 3.9 I haven’t seen them yet. I guess I should check the docs. I’m not a fancy user so it’s probably ok.

          Haskell is worth learning mostly because of how it sharpens your mind. Developing useful programs in it still has obstacles.

          • logging_strict
            link
            fedilink
            arrow-up
            1
            ·
            7 days ago

            dataclasses required py310+ for critical features like slot support. Only now that py39 is history, is dataclasses even a thing. But then a must have feature will require an even more recent python version. So stuck waiting in a never ending loop.

            Fck that!

            python -m pip install attrs

            Problem solved.

            • solrize@lemmy.ml
              link
              fedilink
              arrow-up
              1
              ·
              7 days ago

              I haven’t needed slot support in dataclasses so far. It must not be that critical. Some of my machines still have python 3.9 and dataclasses work, at least to the extent I’ve needed them to.

              • logging_strict
                link
                fedilink
                arrow-up
                1
                ·
                7 days ago

                That’s fine. But you coulda had access to latest greatest updates. Whether you need them or not.

                • solrize@lemmy.ml
                  link
                  fedilink
                  arrow-up
                  1
                  ·
                  7 days ago

                  Python advertises itself as being mature, which means changes if any are supposed to be incremental and backwards compatible. It’s not so successful at that but I still don’t particularly try to keep up with the bleeding edge. I don’t know what slots in dataclasses even means. I do like the match statement.

                  • logging_strict
                    link
                    fedilink
                    arrow-up
                    1
                    ·
                    7 days ago

                    LOL have never used the match statement. Has been one of those features just outta reach. So close can taste it. But alas …

                    Now py310 is here. Still not enthusiastic about it. But over time maybe will come around.

                    __slots__ = (...) or frozen. Same thing. Make the data class read-only reducing memory usage by 30-40%. Like the memory usage difference between tuple and list.

                    From experience, in stubs, __slots__ need to include "__weakref__". If frozen, then don’t include "__weakref__".

                    How about this. Lets meet each other half way.

                    I’ll stop thinking of the match statement as thorium reactor toxic waste.

                    And maybe you can take a read through of the frozen option for dataclasses.

                    Sound fair?

      • logging_strict
        link
        fedilink
        arrow-up
        1
        ·
        11 days ago

        Thanks for the article link. Worried code too much and don’t study enough.

        Appreciate the reading material.