[Python-Dev] PEP 8 updates/clarifications (original) (raw)

Phillip J. Eby pje at telecommunity.com
Mon Dec 12 18:19:24 CET 2005


At 09:59 AM 12/12/2005 -0600, skip at pobox.com wrote:

Jim> That's why, in my suggested writeup, I suggested that attributes Jim> should be used if the accessors are trivial.

In my experience it's difficult to find the locations where another module mucks with your object's state. Using properties or accessor methods coupling between modules is reduced and you can be more confident that the only place an object's state is modified directly is in its own code.

So?

There is no reason for you to care about this in advance of actual requirements. Normal instance variables should be used for normal instance variable things, until you have a need to do something when they change. Then, and only then, is it appropriate to introduce properties. Otherwise, you're just wasting your time with busywork and annoying the heck out of people trying to read your code. Python is not Java, and Java's use of getters and setters is a reflection of its inadequacies as a programming language, not a badge of strength. They're a bug, not a feature.

What would be a nice feature to add to Python would be a descriptor that stores the value of the property in the object dictionary, but calls a function whenever the attribute is changed. So then you could do:

  @setter
  def somevar(self, value):
      # update attrs affected by changing self.somevar

This is the shortest upgrade path for the common case of an attribute's lifetime. First, it's just a regular dict attribute, and then you maybe want to do something when it changes, but you still want it readable and stored normally, without having to have two attribute names (one public and one private).



More information about the Python-Dev mailing list