开发者

Dynamic name resolution

Howcome some languages like PHP and Python use dynamic name resolution?

The only time I've ever thought of using it is to do something like this Python code, to save me from having to explicitly parameters to format:

"{a} {b} {c} {d}".format(**locals())

but it doesn't really take much work to just be explicit (and is a bit less error-prone):

"{a} {b} {c} {d}".format(a=a, b=b, c=c, d=d)

And for setting/getting locals in the same scope, I don't see why anyone would ever use that instead of a map.

Without dynamic name resolution, typos are caught, and you can automatically rename variables without breaking your program (unless something can still read the names of the variables). With dynamic name resolution, you get something that saves you from typing a line? Am I missing something?

Python documentation says they might remove it in the future. Is it more of a historical thing? What's an actual good use case for d开发者_如何学编程ynamic name resolution?


Most dynamically typed languages simply don't have a choice. For an expression like x.y you can't look up y statically, since what fields are available depends on the type of x which is only available at runtime.

There are ways around this (such as type inference or JIT), but since the base language has to have dynamic name lookup, most such languages make it into a feature (see e.g. the power of Lua tables).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜