Could Python supplant Java?

laotseu bdesth at nospam.free.fr
Wed Aug 21 01:06:59 EDT 2002


FISH wrote:
> laotseu <bdesth at nospam.free.fr> wrote in message news:<3D61A538.2050107 at nospam.free.fr>...
> [snip...]
> 
>>I used to think the same (what about VB's 'variant' ? yuk !)... But 
>>python's dynamic typing has got me, specially in the case of oo 
>>programming. It makes polymorphism a really simple thing.
> 
> 
> And more dangerous :-)
> 

What is the more dangerous : relying on the compiler or knowing what you 
do when wrting code ? It's more about the way you work than about the 
language you use.

> 
>>IMHO, compile time type-checking is a nice safety net, but if you get a 
>>type error at compilation, it still means you made a mistake. 
> 
> 
> Yes, but the error is flagged in compilation, as opposed to
> five weeks after the product ships, when it crops up in a bug
> report from a customer.


Programs *should* be tested before shipping
(ok, I know it's a dream but... )

> Strong and weak typing have their place.

What has strong or weak typing has to do with this ? Python is strongly 
typed. The fact that an *identifier* has no type by itself doesn't mean 
that the *object* is refers to doesn't have one.

>  Dynamic types are less
> hassle to work with, but of course they push an added burden of
> testing onto the developer

No. Write (good) tests first, then write your code, then let the 
computer run the tests.

> - because mistakes are not so easily
> spotted.  

Your compiler won't spot most of the mistakes. Well written tests will.

> However, for languages targetted maining at scripting 
> (scripts tend to be brief by their very nature) then the added
> burden of dynamic typing is managable, and the benefits often
> outweigh the problems.
> 
> This is one of the reasons why I do not consider Python a suitable
> alternative to Java.  Dynamic typing is fine for scripts, but when 
> it comes to full-blown 'ten thousand line' applications you really
> should be looking as something IMHO which gives you the added
> security of strong typing, to catch as many potential bugs as
> possible.
> 

Ok. Objective C has dynamic typing. Whole Operating systems have been 
written in Objective C. Would you  consider that :
- Objective C is not suited for anything else than scripting ?
- Java would be a sensible choice for writing an OS ?

(NB : I'm *not* suggesting Python would be a good choice if you plan to 
write an OS)

> 
> 
>>BTW, even 
>>in C, you can have type error when casting a void*, and you don't have 
>>any info about the original type of the pointeur. Dynamic typing doesn't 
>>means Python is not *strongly* typed.
> 
> 
> Surely the two are mutually exclusive?  Unless this is some very
> novel use of the word 'strongly'?  :-)


No. Dynamic typing doesn't not imply weak typing. C has static, but 
relativly weak typing : use a cast, and the compiler will accept anything.

int answer = 42;
char *meaning_of_life = (char *)answer; /* compiler is happy */

(tested with gcc -Wall -pedantic).

So static doesnt means strong.

Now when it comes to 'polymorphism' (or at least some kind of...), C 
uses void *. But once you cast to void *, there is no way to know the 
original type of the pointer :

void funcA(void *meaning_of_life)
{
     /* we just can hope that the caller passed the good type */
     char *marvin = (char *)meaning_of_life;

     /* now cross fingers and run */
     [snip some code that tries & modify marvin[strlen(marvin) - 1] ]
}

void funcB(void)
{
     int answer = 42;

     /*
      * oh, shit, I'm to (lazy || tired || in a hurry) to check
      * the correct type
      */

     funcA(&answer);

     /* system crash ! Too bad */
     ...
}

Get it ?


In Python, when you code :

/> toto = 0 + 0
the identifier toto is bound to an integer object.

Now when you code :

/> toto = 'la tête à toto'
toto is bound to a string object.

The *identifier* toto has no type by itself, but the *object* it is 
bound to does have one, and you can easily know which.

This is, AFAIK, the difference between strong vs weak and dynamic vs 
static typing.

(Please someone correct me if I'm wrong)

Laotseu






More information about the Python-list mailing list