OOP techniques in Python

Duncan Booth duncan.booth at invalid.invalid
Thu Apr 27 13:24:41 EDT 2006


Panos Laganakos wrote:

> i.e. we usually define private properties and provide public functions
> to access them, in the form of:
> get { ... } set { ... }
> 
> Should we do the same in Python:
> 
> self.__privateAttr = 'some val'
> 
> def getPrivateAttr(self):
>     return self.__privateAttr
> 
> Or there's no point in doing so?
> 
If you want to do more than just setting an attribute or returning it then 
define a property, but if you just want to save the value there is no point 
using a property. In some other languages you cannot change an attribute 
into a property without changing the interface to the class, so in those 
languages you need to make everything a property just in case you ever need 
it to be a property in the future.

In Python, when you need it to become a property you can change it without 
breaking anything, so there is no need to obfuscate it prematurely.



More information about the Python-list mailing list