accessing class data members

Steve Holden sholden at holdenweb.com
Tue Jul 3 15:01:55 EDT 2001


"Curtis Jensen" <cjensen at bioeng.ucsd.edu> wrote in message
news:3B420804.9882D166 at bioeng.ucsd.edu...
> In many OO languages, and theory, it is often the case that when you
> want to know the value of some data member, that a function is written
> to return that value.  However, in Python it seems that people just
> access the data member directly.
>
> for example:
> if there were a class that represented a physical object.  In the class
> there were data members that held the objects temperature and location.
> If I wanted to know the temperature of an object, I could just use
> obj.temperature.  Though in other languages, the convention seems to
> call a function that returns the value, like: obj.get_temperature.
>
> Which is better?

I would say oranges are definitely better than apples :-)

In Python you can write accessor methods if you want, but (I believe) most
Python programmers are pragmatic enough to say that this is unnecessary
overhead for simpler, well-understood programs.

Unlike other languages, you can do it either way: an object has effectively
no way of hiding attributes from access by other objects, and so the Python
programmer relies on discipline to ensure everything works. Java and C++,
for example, allow attributes (members) to be private in various ways.

regards
 Steve
--
http://www.holdenweb.com/








More information about the Python-list mailing list