Some recent discussions around the 'net have been tossing around the ideas about static typing in python, briding static and dynamic typing in C++-like languages, and similar concepts of making static-typing more dynamic or dynamic languages more optimized in static-typing ways. Particularly, I was sparked by Michael Feather's "Set of Tests" article. There are different ways we might look into bringing those concepts to Python, and I rolled a few of them around in my head. My final mental landing was "Can we utilize the assert statement to inform the compiler about these tests that are absolute?". Of course, you probably can see how this is a lot like what assert does now, with the only difference being between run-time and compile-time being the target of the rules. This leads us to looking for where an assert could be compile-time verified and then used to optimize code. The most basic compile-time assert I can think of us "assert builtin is builtin", which would be a contract that the name 'builtin' will continue to be bound to the default builtin object, and won't be changed. This means we can do "assert isinstance is isinstance" and the compiler can make assumptions it could not before: that when it sees the name isinstance, it knows exactly what it is before runtime. This opens up other expressions that use these known names and promise other things to the compiler. We could do things like "assert isinstance(l, sequence)" or "assert len(l)==3", which would create a pair of contracts that l was some kind of 3 element sequence, and the compiler could make it a tuple for optimization.
At a small suggestion in #python, I wrote up a simple module that allows the use of many python statements in places requiring statements. This post serves as the announcement and documentation. You can find the release here . The pattern is the statement's keyword appended with a single underscore, so the first, of course, is print_. The example writes 'some+text' to an IOString for a URL query string. This mostly follows what it seems the print function will be in py3k. print_("some", "text", outfile=query_iostring, sep="+", end="") An obvious second choice was to wrap if statements. They take a condition value, and expect a truth value or callback an an optional else value or callback. Values and callbacks are named if_true, cb_true, if_false, and cb_false. if_(raw_input("Continue?")=="Y", cb_true=play_game, cb_false=quit) Of course, often your else might be an error case, so raising an exception could be useful
Comments