Over a million developers have joined DZone.

What's the Point of Properties in Python?

DZone's Guide to

What's the Point of Properties in Python?

· Web Dev Zone ·
Free Resource

Building real-time chat? Enroll in a Free Course on Mobile Chat Development. 

A few days ago I was asked by a collegaue what the point of properties in Python is. After all, writing properties is as much text as writing getters and setters, and they don't really add any functionality except from not having to write '()' on access.

On the surface, this argument holds as we can see by comparing a simple class implemented with getters and setters, and with properties.

Implemented with getters and setters:

>>> class GetSet(object):
...   x = 0
...   def set_x(self, x):
...     self.x = x
...   def get_x(self):
...     return self.x

>>> getset = GetSet()
>>> getset.set_x(3)
>>> getset.get_x()

And implemented with properties:

>>> class Props(object):
...   _x = 0
...   @property
...   def x(self):
...     return self._x
...   @x.setter
...   def x(self, x):
...     self._x = x
>>> props = Props()
>>> props.x = 5
>>> props.x

The point

In fact, we've gone from 196 to 208 chars in this simple use case - so why would we use properties at all?

The answer is, that in this use case we would not. In fact, we would write thus:

>>> class MyClass(object):
...   x = 0
>>> my = MyClass()
>>> my.x = 4
>>> my.x

'But!', I can hear you scream, 'there's no encapsulation!'. What will we do if we need to control access to x, make it read-only or do something else to it? Won't we have to refactor everything to the getters and setters that we avoided?

No - we just switch to the property version, add whatever we want, and have not changed the interface one iota! The great thing about properties is not that they replace getters and setters, its that you don't have to write them to future-proof your code. You can start out by writing the simplest implementation imaginable, and if you later need to change the implementation you can still do so without changing the interface. Neat, huh?


Source: http://blaag.haard.se/What-s-the-point-of-properties-in-Python/

Power realtime chat, IoT and messaging apps at scale. Pubsub realtime messaging, functions, chat, presence, push, notifications, blocks catalog and more.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}