Pretty please: Named arguments
Bekenn
leaveme at alone.com
Wed Mar 2 10:38:05 PST 2011
On 3/2/11 8:45 AM, spir wrote:
> I had never thought at that, but I'm surprised: what prevents Python's
> "compiler" (say, a semantic phase after parsing) to check number and
> names of arguments. (Number seems not to be checked before runtime
> neither.)
> All required information is in the AST. For named params, Python could
> translate to position params just like D. This would certainly remove a
> relevant amount of runtime "speed-down", I guess. (Only type-check of
> builtin func args must remain at runtime.)
>
> Denis
What follows is speculation; I'm not a Python programmer, but I am
loosely familiar with the language. If I'm completely wrong, I'm sure
someone will point it out:
A Python "compiler" certainly can (and probably does) check function
arguments, but the runtime is still heavily involved in argument
passing. The complexity in Python is an artifact of how arguments are
delivered at run-time: in a big (dynamically created, of course)
dictionary. Essentially, The interpreter matches argument positions
with parameter names -- at run time -- and then supplies those
name/argument pairs as entries in the dictionary. (This is a logical
view of the language, and may not precisely match implementations. For
instance, I'd expect that "compiled" .pyc files throw out positional
information ahead of time.) Looked at another way, *everything* is
passed by name, not position; positional arguments are merely a
shorthand notation.
This is vastly different from how named arguments would be handled in D.
D passes arguments by position -- end of story. Named arguments
provide an alternative method of specifying the position of a given
argument, which is determined by the compiler long before the linker or
the runtime get involved.
More information about the Digitalmars-d
mailing list