812
submitted 1 day ago by [email protected] to c/[email protected]
you are viewing a single comment's thread
view the rest of the comments
[-] [email protected] 1 points 7 hours ago* (last edited 7 hours ago)

I would do

class MyClass:
    def __init__(self, value):
        self._whatever = value

    @property
    def whatever(self):
        return self._whatever

Personally, I would type hint all of that but I'm just showing how you can do it without types. Your linter should be smart enough to say "hey dumbass did you mean this other thing"? Also since we didn't create a setter you can't arbitrarily overwrite the value of whatever so thats neat.

And I'll just say before I post that I'm on mobile and I'm sorry if the formatting is fucked. I'm not going to fix it.

this post was submitted on 25 Sep 2024
812 points (97.3% liked)

Programming

17071 readers
408 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS