<div class="gmail_quote">On 3 January 2012 20:38, Terry Reedy <span dir="ltr"><<a href="mailto:tjreedy@udel.edu" target="_blank">tjreedy@udel.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div>On 1/3/2012 1:13 PM, Sean Wolfe wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I have a theoretical / philosophical question regarding strong vs duck<br>
typing in Python. Let's say we wanted to type strongly in Python and<br>
</blockquote>
<br></div>
Python objects are strongly typed, in any sensible meaning of the term.<br>
What you mean is statically or explicitly typing names.<div><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
were willing to compromise our code to the extent necessary, eg not<br>
changing variable types or casting or whatever. Let's say there was a<br>
methodology in Python to declare variable types.<br>
<br>
The question is, given this possibility, would this get us closer to<br>
being able to compile down to a language like C or C++?<br>
</blockquote>
<br></div>
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.</blockquote><div>


<br></div><div>A minor point of clarification here: although what you wrote is technically true, it's also misleading.</div><div><br></div><div>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.</div>

<div><br></div><div>Shedskin <b>truly</b> compiles to C and thus is afflicted with several restrictions such as static typing.</div><div><br></div><div>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, <b>the computer natively parses the bytecode which tells CPython what to do</b>. This reduces a lot of overhead in tight loops, but doesn't help much in the general case.</div>

<div><br></div><div>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 ;)</div>

<div><br></div><div>--------------------------------------</div><div><br></div><div>To  answer Devin:</div><div>You're probably thinking of Cython ^^.</div></div>