Could Python supplant Java?

James J. Besemer jb at cascade-sys.com
Fri Aug 23 21:41:18 CEST 2002


Bernhard Herzog wrote:

> "James J. Besemer" <jb at cascade-sys.com> writes:
>
> > Casts are NOT automatically generated for pointers, so the following
> > code WILL exhibit the expected Polymorphism without virtual functions
> > (classes A and B as above):
> >
> >     void foo( A* arg ){
> >         arg.x;
> >         arg->bar();        // calls A or B depending on type of arg
> >     }
> >
> >     A a; B b;
> >
> >     foo( &a );    // foo will call A.bar()
> >     foo( &b );    // foo will call B.bar()
>
> No, foo(&b) will call A::bar unless bar is virtual.

I stand corrected.

You do need to add 'virtual' for this kind of polymorphism.

I ran the code to test it but neglected to take out 'virtual' from an earlier
experiment.

> However, which virtual method bar is called in a function like this
>
> void foo(A * a)
> {
>     a->bar();
> }
>
> Is *not* determined at link time.

Yes and no.  The destination address for each possible object type, IS
determined at link time.  There are finite number of specific alternatives,
one for each instance type.  Which one gets called is finally determined by
the object type of a at runtime.  The alternatives are narrowed down to a
specific address in the same relative slot in each instance's vtab.  There is
no symbol table for "late binding" to take place with.  It's just an extra
level of indirection.

The generated code is the logical equivalent of a switch statement, not a hash
table lookup, which is why I maintain it's "early binding".  It runs LOTS
faster than a Python or Smalltalk function call.  In exchange for performance
and compile time type checking, you give up some of the "late binding"
goodies.

If you disagree with my characterization, then let's turn it around.

The conclusion then is that languages such as C++ and Delphi are "late
binding" languages and provide full Polymorphism THAT way.

Either way, there's nothing unique about Python's OOP facilities which was my
main point.  In fact, as pointed out earlier, Python lacks a few nice things
found in C++, Delphi, and Smalltalk like function and operator overloading.

Regards

--jb

--
James J. Besemer  503-280-0838 voice
http://cascade-sys.com  503-280-0375 fax
mailto:jb at cascade-sys.com






More information about the Python-list mailing list