DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > What's the Point of Properties in Python?

What's the Point of Properties in Python?

Fredrik Håård user avatar by
Fredrik Håård
·
Jan. 23, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
3.01K Views

Join the DZone community and get the full member experience.

Join For Free

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()
3

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
5

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
4

'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/

Property (programming) Python (language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Modern Application Security Requires Defense in Depth
  • Exhaustive JUNIT5 Testing with Combinations, Permutations, and Products
  • Build a Data Pipeline on AWS With Kafka, Kafka Connect, and DynamoDB
  • JIT Compilation of SQL in NoSQL

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo