use member functions to access data in Python classes?

Peter Hansen peter at engcorp.com
Wed Jun 18 12:45:39 EDT 2003


beliavsky at aol.com wrote:
> 
> In C++, it is generally recommended to declare the data within a class
> private and to access the data using member functions. Do experienced
> Python programmers usually use member functions to access data within
> classes, or do they access the data directly?

I follow the conventional approach used even by the Python libraries,
which have proven to be quite effective :-), by just accessing most
data directly.

If the type of access involved is more complicated that getting or 
setting, I naturally provide a method to perform the operation 
instead.

Note that the latest versions of Python have "properties" which will
allow you to combine the best (?) of both worlds by writing code as
though it had direct access but which actually calls methods under
the table.

-Peter




More information about the Python-list mailing list