An alternative approach to bound methods

Alex Martelli aleaxit at yahoo.com
Sat Feb 24 13:05:29 EST 2001


"Marcin 'Qrczak' Kowalczyk" <qrczak at knm.org.pl> wrote in message
news:slrn99ffp6.ep3.qrczak at qrnik.zagroda...
    [snip]
> > People will often and erroneously make recursive calls to
> > "amethod(a,b,c)" when they should be going through self, because
> > "going through self" IS the normal, most frequently desired case.
>
> Both are equally easy. Why would anybody write
>     amethod(self,b,c)
> instead of
>     self.amethod(b,c)
> when he means the latter?

The pragmatical reason for which people erroneously omit 'self'
(and try to call amethod(a,b,c), not amethod(self,a,b,c)) from
within the code for a method is no doubt that there are many
languages in which this "self." (or its equivalent "this->") can be
left implicit.  But we can't change those other languages (C++,
Java, ...), so, people who come from such languages and/or
use them primarily, and only use Python more rarely, _will_ no
doubt keep making this mistake frequently -- even though in
theory there should be no intrinsic reason why they should.

The key point is that we need to keep diagnosing this error
at runtime clearly and unambiguously, just like Python does
now -- this is and will remain an important pragmatical issue,
and pragmaticity trumps purity.


> > I don't want this error to become a silent, mysterious one; we know
> > have clear diagnostics for it, and losing them would be a serious
> > step backwards.
>
> I admit that this error would have a worse diagnostics (usually:
> too few arguments, i.e. not that bad).

Much worse if the method has at least one optional argument,
which is a VERY frequent case; then, we've pushed the error
into the dark realm of mysteriously erroneous runtime behavior

> The same can be said about
> writing 'self' in method definitions: when one forgets it, the error
> is equally mysterious.

My impression, which comes from working directly with a modest
number of Python newbies and helping many more through the
net, is that the error of omitting self in the _definition_ is much
rarer than that of omitting it in the _calls_ to methods within other
methods.  And when you make that mistake in one method out
of N, getting diagnostics on _every_ call to that method (and for
every attempted use of self within that method -- if one makes
TWO mistakes, omitting self in definition AND in use, then of
course it's not easy to diagnose both correctly & clearly -- but
the single-error case is more important) tends to pinpoint the
error pretty clearly.


Alex






More information about the Python-list mailing list