hanumizzle at gmail.com
Sat Oct 21 10:29:15 CEST 2006
On 21 Oct 2006 01:17:32 -0700, phez.asap at gmail.com <phez.asap at gmail.com> wrote:
> I am new to Python but come from a C++ background so I am trying to
> connect the dots :) . I am really liking what I see so far but have
> some nubee questions on what is considered good form. For one thing I
> am used to class variables being accessable only through methods
> instaed of directly refrenced from the object instence. From what I
> have read it looks like when you access a class variable directly in
> Python it has something in the background that works similar to a
> getter of setter.
> Would you normally write methods to retrive and set your class
> variables or just refrence them directly?
It's largely a matter of taste and context I think. For instance, you
can trust the user of your code to leave read-only variables read-only
in most cases. I don't think there really is any absolutely sure way
of introducing private attributes (__ prefix just name-mangles). In
other cases, it seems more logical to have 'virtual' attributes, a few
canonical examples being temperature conversion and exchange rates;
imagine an object that has a number of dollars, Euros, yen, or other
major currency as an attribute and uses methods to give the equivalent
value in other currencies.
More information about the Python-list