• jollyrogue@lemmy.ml
    link
    fedilink
    arrow-up
    4
    ·
    14 days ago

    Aren’t setters and getters discouraged in Python?

    I remember reading something like, “This isn’t C++ , and Python doesn’t have private vars. Just set the var directly.”

    • TheDoctor [they/them]@hexbear.net
      link
      fedilink
      English
      arrow-up
      2
      ·
      14 days ago

      In the way that’s common in languages like Java where you’re making a property read-only, yes. But there’s a whole protocol in Python called descriptors where you can override the . on a field. The most common form of these is class methods annotated with the @property annotation, which makes it so the method can be accessed as if it were a property.