Another try at Python's selfishness

Nick Craig-Wood nick at craig-wood.com
Sat Feb 4 12:30:04 EST 2006


Terry Hancock <hancock at anansispaceworks.com> wrote:
>  On Thu, 02 Feb 2006 19:27:55 -0600
>  DH <no at spam.com> wrote:
> > But I think most people who don't like the extraneous
> > 'self' in python  just consider it a minor inconvenience
> > and don't even notice it after  using python for a while. 
> 
>  After messing around with Javascript (many magical
>  variables that suddenly show up in your namespaces!), I
>  began to *seriously* appreciate Python's design.  Having
>  self as an explicit parameter is beautiful self-documenting
>  design.

I have to agree...

Here is my experience with the same idea in C++...

I've converted lots of C code written in an object oriented style into
C++.  In C you pass round a pointer to a struct, which gets
transformed into the new class on conversion.  Typically the code is
then full of this->member = that, etc.  This is the python equivalent
of self.member = that.  At this point I usually remove all the this->
from the code which then causes it to malfunction horribly at run-time
(it compiles fine) because of all the name space clashes between the
things in the class and the local variables.  The compiler doesn't
warn about this - its perfectly legal C++.

In a lot of big C++ programs various conventions are used to try to
help with this - naming all parameters to functions _name rather than
name, or using this->member rather than member.  The python way of
enforcing self.member is much cleaner and never comes back to bite you!

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list