Search code examples
pythonclasspropertiesdecoratorpython-decorators

Python - multiple @property statements in class definition?


Getting up to speed on learning classes. I have been reading that the constructor (def init in Python) should only set assigned variables, that calculated instance attributes should be set via a property. Also, that using @property is preferred to a Java-style getter/setter.

OK, but every example I have ever seen on this sets only one property. Let's say I have an object with three complicated attributes that need to be calculated, queried etc. How do you represent multiple @property getters, setters, deleters? Here is an example from another post:

class C(object):
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

So if I had three instance variables that were calculated values based on some other attributes, would it look like this

class C(object):
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

    @property
    def y(self):
        """I'm the 'y' property."""
        return self._y

    @y.setter
    def y(self, value):
        self._y = value

    @y.deleter
    def y(self):
        del self._y

    @property
    def z(self):
        """I'm the 'z' property."""
        return self._z

    @z.setter
    def z(self, value):
        self._z = value

    @z.deleter
    def z(self):
        del self._z

Or is that fact that I only ever see one @property statement mean that having a class with more than one @property is a bad idea?


Solution

  • No, you can use multiple @property decorators to your heart's content. There is no limit here, other than that of example writers imaginations, apparently.

    The Python standard library is full of @property use if you want examples:

    • numbers defines ABCs for the numbers classes in Python.

    • tempfile implements temporary file objects

    • threading provding higher-level thread support

    • urlparse for handling URLs and query strings.

    etc.

    You had it spot on; multiple properties would look exactly like what you posted.