python philosophical question - strong vs duck typing

Joshua Landau at
Tue Jan 3 16:25:20 EST 2012

On 3 January 2012 20:38, Terry Reedy <tjreedy at> wrote:

> On 1/3/2012 1:13 PM, Sean Wolfe wrote:
>  I have a theoretical / philosophical question regarding strong vs duck
>> typing in Python. Let's say we wanted to type strongly in Python and
> Python objects are strongly typed, in any sensible meaning of the term.
> What you mean is statically or explicitly typing names.
>  were willing to compromise our code to the extent necessary, eg not
>> changing variable types or casting or whatever. Let's say there was a
>> methodology in Python to declare variable types.
>> The question is, given this possibility, would this get us closer to
>> being able to compile down to a language like C or C++?
> Cython compiles Python as is to C. It also gives the option to add type
> annotations to names to get faster code. Shredskin compiles a subset of
> Python, or a subset of usages, to C, with similar benefits.

A minor point of clarification here: although what you wrote is technically
true, it's also misleading.

AFAIK both Cython and Shedskin compile only a subset of Python, but the
magnitude difference they drop off is large. Almost everything is Cython
compliant, and that should just grow, but there are a few flakes here and
there. However, Shedskin is a well defined subset, and does not encompass
the whole of Python and will never do so.

Shedskin *truly* compiles to C and thus is afflicted with several
restrictions such as static typing.

Cython is a lot less clear. What it compiles to is basically bytecode
written in C. Instead of CPython reading the bytecode and the bytecode
being parsed and then executed, *the computer natively parses the bytecode
which tells CPython what to do*. This reduces a lot of overhead in tight
loops, but doesn't help much in the general case.

So why sacrifice so much for so little gain? Well - when your bytecode is
C, you can interoperate other C code right inside. If you make some C
variables you can do C-math inside your Python with no real overhead.
Additionally, you can call C libraries really easily inside this code. What
it results in is a really easy way to optimise the 5% that's slowing your
code, and instead of rewriting it in C you use C-with-python-syntax. The
types are sill horrible, though ;)


To  answer Devin:
You're probably thinking of Cython ^^.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list