What's the best way to minimize the need of run time checks?

Steven D'Aprano steve+python at pearwood.info
Sun Aug 14 10:29:29 EDT 2016


On Thu, 11 Aug 2016 06:33 am, Juan Pablo Romero Méndez wrote:

> I've been trying to find (without success so far) an example of a
> situation where the dynamic features of a language like Python provides a
> clear advantage over languages with more than one type.

Python has more than one type. Don't confuse dynamic typing with weak typing
or untyped (typeless) languages. More on this below.

I don't believe that you will find "an example of a situation..." as you say
above. It sounds like you are hope to find a clear example of "If you do
This, then dynamic languages are the Clear Winner". But I don't think you
will. Dynamic languages tend to produce clear productivity improvements
over statically typed languages, but of course this is only "typically"
true, not a guarantee that applies to every single programmer or project.

Typically:

- dynamic languages are less verbose;
- dynamic languages are faster to develop in; many organisations 
  prototype applications in Python (say) before re-writing it in
  C++/Java/whatever;
- far less time spent fighting the compiler;
- dynamic languages often have fewer bugs, because it is easier to 
  reason about the code (no "undefined behaviour" like in C!) and 
  fewer lines of code to reason about;
- but statically typed languages allow you to prove the absence 
  of certain types of bugs.

The exception is if you try to write statically typed code in a dynamic
language. Then you get the worst of both styles of coding: the verbose,
heavyweight style of many static languages, but without the automated
correctness proofs, plus the performance costs of dynamic typing, but
without the rapid development.



Regarding types and type systems, if you haven't already read this, you
should:

https://cdsmith.wordpress.com/2011/01/09/an-old-article-i-wrote/

"Static typing" (e.g. Pascal, C, Java, Haskell) and "dynamic typing" (e.g.
Python, Javascript, Ruby, Lua) differ on when and how values are checked
for type-compatibility.

"Strong" and "weak" typing are ends of a continuum. Nearly all languages are
a little bit weak (they allow automatic coercions between numeric types)
but mostly strong (they don't automatically coerce integers to arrays).
Javascript, Perl and PHP are weaker than Python because they'll coerce
strings to numbers automatically and Python won't.

I don't know many untyped languages apart from machine code or maybe
assembly. Perhaps Forth? (Maybe not -- some Forths include a separate
floating point stack as well as the usual stack.) Hypertalk treated
everything as strings. Tcl treats nearly everything as strings, although it
also has arrays.

So, Python has types, and it is a mostly strong typed language. It will do
relatively few automatic coercions.



-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list