Attack a sacred Python Cow

Derek Martin code at pizzashack.org
Sun Jul 27 15:59:26 EDT 2008


On Sun, Jul 27, 2008 at 08:19:17AM +0000, Steven D'Aprano wrote:
> > You take the name down to a single letter. As I suggested in an earlier
> > post on this thread, why not take it down to zero letters? 
> 
> The question isn't "why not", but "why". The status quo works well as it 
> is, even if it isn't perfect. Prove that implicit self is a good idea -- 
> or at least prove that it is an idea worth considering.

Come on, this sounds like a schoolyard argument.  This comes down to a
matter of style, and as such, is impossible to prove.  It's largely a
question of individual preference.

That said, the argument in favor is rather simple:

1. This is an extremely common idiom in Python
2. It is completely unnecessary, and the language does not suffer for
   making it implicit
3. Making it implicit reduces typing, reduces opportunities for
   mistakes, and arguably increases consistency.

As for the latter part of #3, self (or some other variable) is
required in the parameter list of object methods, however when the
method is *called*, it is omitted.  It is implied, supplied by Python.
Thus when an object method is called, it must be called with one fewer
arguments than those which are defined.  This can be confusing,
especially to new programmers.

It can also be argued that it makes the code less ugly, though again,
that's a matter of preference.

> It's not enough to show that a change "isn't bad" -- you have to show 
> that it is actively good. 

But he did... he pointed out that *it saves work*, without actually
being bad.  Benefit, without drawback.  Sounds good to me!

> "Don't need to look at the method signature" is not an argument in favour 
> of implicit self. 

Yes, actually, it is.  If there is a well-defined feature of Python
which provides access to the object within itself, then the
opportunities for mistakes when someone decides to use something else
are lessened.

> You don't need to look at the method signature when you're using an
> explicit self either.

That isn't necessarily true.  If you're using someone else's code, and
they didn't use "self" -- or worse yet, if they chose this variable's
name randomly throughout their classes -- then you may well need to
look back to see what was used.

It's bad programming, but the world is full of bad programmers, and we
don't always have the choice not to use their code.  Isn't one of
Python's goals to minimize opportunities for bad programming?
Providing a keyword equivalent to self and removing the need to name
it in object methods is one way to do that.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20080727/2f60247c/attachment-0001.sig>


More information about the Python-list mailing list