Evaluating python - a question
Alex Martelli
aleaxit at yahoo.com
Fri May 18 04:31:57 EDT 2001
"Just van Rossum" <just at letterror.com> wrote in message
news:3B04D1F9.875E4C2E at letterror.com...
> Christian Tanzer wrote:
>
> > > Other than that, the suggestion to use accessor methods, instead of
> > > accessing the datamembers directly, is probably another good tip.
> >
> > Accessor methods won't do anything to prevent typos -- you have an
> > equal chance of mistyping the name of the accessor as mangling the
> > attribute name.
>
> Equal chance of mistyping, yes, but the point was that a typo in an
> accessor method triggers an exception, whereas simply setting an
> attribute does not:
>
> class Foo:
> def __init__(self):
> self.foo = 1
> def setFoo(self, value):
> self.foo = value
>
> f = Foo()
> f.fooo = 2 # no exception
> f.setFooo(2) # boom
A terminology issue: this seems to be about MUTATOR methods rather
than ACCESSOR ones. ACCESSING a non-existent attribute raises an
exception just as much as calling a non-existent accessor method.
A *MUTATOR* (setSomething rather than getSomething) call does raise
an exception on mis-spelling while assigning to an attribute reference
would not (unless one had a suitable __setattr__ method of course).
Alex
More information about the Python-list
mailing list