What is the naming convention for accessor of a 'private' variable?

Gregory Ewing greg.ewing at canterbury.ac.nz
Fri Nov 20 20:16:52 EST 2009


Steven D'Aprano wrote:
> On Wed, 18 Nov 2009 18:47:34 -0800, Chris Rebert wrote:
> 
>>Accessor methods are not Pythonic. 
> 
> Accessor methods are *usually* not Pythonic, at least not the way they 
> are commonly used in Java.

To elaborate, accessor methods that *only* read and write
another, ordinary attribute are completely unnecessary
in Python. In that case there is no harm in exposing the
underlying attribute directly, because you can always
replace it with a property later if you change your mind,
without requiring any change in calling code.

Also, even when you do need accessor methods, it's more
elegant to hide them behind a property than to expect
callers to use them directly. It makes the calling code
more concise, and allows for changing your mind in the
opposite direction.

-- 
Greg



More information about the Python-list mailing list