[EMAIL PROTECTED] wrote:
> When I look at how classes are set up in other languages (e.g. C++),
I
> often observe the following patterns:
> 1) for each data member, the class will have an accessor member
> function (a Get<whatever> function)
> 2) for each data member, the class will have a mutator member
function
> (a Set<whatver> function)
> 3) data members are never referenced directly; they are always
> referenced with the accessor and mutator functions
>
> My questions are:
> a) Are the three things above considered pythonic?

No.  It's not good programming practice in C++, either.

If you have a class that's nothing but a big data structure, you ought
to use it as a data structure.  Writing accessor and mutator methods
for its fields is just doing a lot of work to accomplish nothing.

If you want to provide access to a certain occasional field, but you're
concerned about keeping the interface backwards-compatible, go ahead
and use them.  But try to observe the following rules of thumb:

1. Don't provide accessor or mutator function to every single member of
every single class you write.  Only provide accessor/mutator functions
if the accessor/mutator methods are a sensible and useful part of the
class's interface.

2. Don't think of these methods as accessors or mutators.  Instead,
think
of them as methods that access or mutate a certain abstract property of
the object that happens to be represented by a single member.

And, keep in mind that, since Python doesn't really have private data,
you don't have to worry about adding these functions to make debugging
easier.


> b) What are the tradeoffs of using getattr() and setattr() rather
than
> creating accessor and mutator functions for each data member?

Don't use getattr and setattr unless you have to construct the name of
the attribute at run time.  That's what they're for.


-- 
CARL BANKS

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to