[Python] Properties vs. generic setter/getter and descriptor

Discussão em 'Python' iniciado por Stack, Fevereiro 1, 2025.

  1. Stack

    Stack Membro Participativo

    I can't seem to find a definitive answer on the matter and I guess the reason is because it depends on the situation.

    a, b and c (and d, e, f... as only 3 attributes are listed in this example for simplicity purposes, which probably tells that the approach is wrong if a class needs that many attributes) are different attributes but have some similarities, which probably means that splitting them in other classes would be the better choice.

    Which one of the following options is the most "Pythonic" or efficient when creating a class with multiple "private" attributes? Or would a different approach (e.g. grouping similar attributes in separate classes) be better? How'd that look if, let's say, b and c are the same "kind" of attribute? (e.g. b being an input path and c being an output path).

    Multiple property decorators and setters:

    class A: # Too many properties and setters?

    def __init__(self, a, b, c):
    self._a = a
    self._b = b
    self._c = c

    @property
    def a(self):
    return self._a

    @a.setter
    def a(self, value):
    self._a = a

    @property
    def b(self):
    return self._b

    @b.setter
    def b(self, value):
    self._b = b

    ... # (same for c)


    or multiple setters/getters with "private" attributes:

    class A: # Name mangling issues?

    def __init__(self, a, b, c):
    self.__a = a
    self.__b = b
    self.__c = c

    def set_a(self, a):
    self.__a = a

    def get_a(self):
    return self.__a

    def set_b(self, b):
    self.__b = b

    def get_b(self):
    return self.__b

    ... # (same for c)


    or generic setters/getters:

    class A: # Less cluttered, but less intuitive that attributes are meant to be "private"?

    def __init__(self, a, b, c):
    self.a = a
    self.b = b
    self.c = c

    def __getattr__(self, name: str):
    return self.__dict__[f"_{name}"]

    def __setattr__(self, name, value):
    self.__dict__[f"_{name}"] = value


    or a Python Descriptor? (How'd that'd look like if b and c were to be moved to a separate class as they share some similarities?).

    Thanks!

    Continue reading...

Compartilhe esta Página