Why the 'self' argument?

John Roth newsgroups at jhrothjr.com
Mon Sep 8 16:52:38 EDT 2003


"Harri Pesonen" <fuerte at sci.fi> wrote in message
news:7T47b.4753$ZB4.113 at reader1.news.jippii.net...
> John Roth wrote:
>
> > "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
> >
> >>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.
>
> Thanks guys, I hadn't heard of __slots__ before. Perhaps I am reading a
> too old book. Of course, a compile time check would be better...

__slots__ came in with 2.2 new style classes, and it's definitely
an advanced feature. There's also some thought that it may not
be the best way to do the job.

> >>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.
>
> I think that Python already has run time type checking. It does not
> allow "a" + 1, for example.

Actually, it doesn't. It has run time strong typing, which is a different
thing.

To me, at least, "type checking" implies a meta-operation, that is,
something that is outside of the operation itself insuring that the correct
types are used. In languages with static typing, that something is the
compiler.

Runtime type checking means that there is additional code somewhere
that checks that the object you are trying to bind is of an allowable
type; that code is basically supervisory in nature.

> > 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.
>
> It would be quite simple to have "option explicit" like in Visual Basic,
> I believe. Like the following:
>
> option explicit
> var a = "asdf"
> b = 1 # error at compile time
> a = 123
> (var b, var c, var d) = (1, 2, 3)
> e = "asdf" # error at compile time

Things are not so simple. To do compile time type
checking, the compiler has to know the actual type
of the result of the expressions on the right hand side
of the assignments. In a dynamic language like Python,
that's not possible without very fundamental changes
to the nature of the language.

It's not a simple thing to do, and the fact that ML
family languages manage to do it without having
to have explicit type declarations (well, most of the
time anyway) is a major achievement, IMO.

The fact that Bicycle Repair Man manages to do it
as well as it does for Python is an even more
impressive achievement.

John Roth

>
> Harri
>






More information about the Python-list mailing list