Why the 'self' argument?

John Roth newsgroups at jhrothjr.com
Mon Sep 8 13:23:44 EDT 2003


"Harri Pesonen" <fuerte at sci.fi> wrote in message
news:TRr6b.4056$ZB4.1410 at reader1.news.jippii.net...
> Grant Edwards wrote:
>
> > In article <Ucp6b.3992$ZB4.3874 at reader1.news.jippii.net>, Harri Pesonen
wrote:
> >
> >>I agree, it's not logical. I'm learning Python at the moment, and like
> >>it very much. This "self" thing seems to be the only odd feature,
> >
> > It seemed quite natural to me, but perhaps that's because I'd
> > used other languages that worked the same way.  Coming from
> > Modula-3 and Smalltalk, the way classes worked in Python seemed
> > quite intuitive.
> >
> > OTOH, C++ seems like a real non-intuitive mess to me.
> >
> >>it feels like the whole class feature was added later.
> >
> > Why?
>
> Because everything else in Python seems to be very compact, there are no
> variable type declarations, or variable declarations, or anything else
> unnecessary that can be omitted. I would like to have self omitted, it
> would make the class syntax more beautiful and compact. On the other
> hand, I would like to have real private methods.
>
> Also I think that the class members should be explicitly declared. In
> general, it would be nice to have an option (like Option Explicit in
> Visual Basic) so that you can't assign to variables that have not been
> declared. It would probably make Python less error prone.

I believe that is what __slots__ is for. That's run time rather than
compile time, but at least it gives you the error on the statement
that attempts to do an invalid assignment.

> Also if
> variable declarations could have the type, again some errors could be
> detected at compile time.

It's quite possible to add run time type checking to Python, at a
significant cost in performance. See the descriptors material.

There's been quite a bit of work on a compile time type checking
system over the years, but it has never resulted in a proposal that could
be agreed on.

My own personal opinion is that I like the type check system in
the ML family of languages: it stays out of your way unless you need it.
How that would fit in Python is another question, though.
At this time, it looks like an issue for Python 3.0, which seems to
keep receeding into the distance.

John Roth

> I have not written any real Python applications yet, so I'm not expert,
> these are just my thoughts at this point.
>
> Harri
>






More information about the Python-list mailing list