动态名称解析 [英] Dynamic name resolution

查看:55
本文介绍了动态名称解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP和Python等某些语言如何使用动态名称解析?

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

我唯一想过使用它的方法是执行类似以下Python代码的操作,以免我不必为format显式指定参数:

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文档说,他们将来可能会删除它.这更多是历史性的吗?动态名称解析的实际好用例是什么?

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 dynamic name resolution?

推荐答案

大多数动态类型化的语言根本没有选择.对于类似x.y的表达式,您不能静态地查找y,因为可用的字段取决于x的类型,而x的类型仅在运行时可用.

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.

有许多解决方法(例如类型推断或JIT),但是由于基本语言必须具有动态名称查找功能,因此大多数此类语言都将其纳入功能(例如,参见Lua表).

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).

这篇关于动态名称解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆