Hershey is right about this language. But to add specific answers to the following language:
In python, an instance variable is an instance attribute (usually) that is mentioned in the instance dictionary. This is similar to elements or instance variables in Java, except that everything is open.
Properties are shortcuts to getter / setter methods that look exactly like an instance variable. Thus, in the following class definition (modified from Guido, the manifest of the new style style ):
class C(object): def __init__(self): self.y = 0 def getx(self): if self.y < 0: return 0 else: return self.y def setx(self, x): self.y = x x = property(getx, setx) >>> z = C() >>> zx = -3 >>> print zx 0 >>> print zy -3 >>> zx = 5 >>> print zx 5 >>> print zy 5
y is an instance variable z , x is a property. (In the general case, when a property is defined, there are some methods used to obscure the associated instance variable so that other code does not directly access it.) The advantage of properties in python is that the designer does not need to go around pre-encapsulating all instance variables, since future encapsulation by converting the instance variable to a property should not violate the existing code (unless the code uses loopholes that your encapsulation is trying to fix, or assume to check the class or some other meta-programming).
All this is a very long answer, to say that at the design level it’s good to talk about properties. An agnostic about what type of encapsulation you may need to perform. I assume that this principle is not an agnostic of the language, but it applies to languages next to python.
David berger
source share