Pretty please: Named arguments
Steven Schveighoffer
schveiguy at yahoo.com
Wed Mar 2 10:48:15 PST 2011
On Wed, 02 Mar 2011 13:38:05 -0500, Bekenn <leaveme at alone.com> wrote:
> 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.
Not a python expert either, but I think you are right.
Most dynamic languages are this way -- because you need that flexibility
since a "compiled" script might not be recompiled after you change the API.
And also, many dynamic languages allow changing the API mid-program (not
sure about python). There would just be no way to determine this at
compile time.
-Steve
More information about the Digitalmars-d
mailing list