quantifiedcode / python-anti-patterns

An open collection of Python anti-patterns and worst practices.
https://quantifiedcode.github.io/python-anti-patterns
Other
1.71k stars 249 forks source link

Properties could be a best practice? #105

Closed romeroyonatan closed 5 months ago

romeroyonatan commented 7 years ago

In the pattern Accessing a protected member from outside the class could be use properties a best practice?

For example

class Rectangle(object):
    def __init__(self, width, height):
        self._width = width
        self._height = height

    @property
    def width(self):
        return self._width

r = Rectangle(5, 6)
# access from property
print("Width: {:d}".format(r.width))

In this way, we ensure read-only access to member and avoid side-effects because the assignment is not allowed