Python描述符协议在其他语言中的模拟?
是否有类似Python描述符协议的东西用其他语言实现? 这似乎是一种增加模块化/封装性而不会使包含类的实现膨胀的好方法,但我从未听说过任何其他语言中有类似的事情。 由于查找开销,其他语言是否可能不存在这种情况?
Is there something like the Python descriptor protocol implemented in other languages? It seems like a nice way to increase modularity/encapsulation without bloating your containing class' implementation, but I've never heard of a similar thing in any other languages. Is it likely absent from other languages because of the lookup overhead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ruby 和 C# 都可以让您通过为属性指定 getter/setter 方法来轻松创建访问器,就像在 Python 中一样。 然而,这并不是为了让您自然地以 Python 允许的方式在另一个类中编写这些方法的代码。 在实践中,我不确定这有多重要,因为每次我看到通过描述符协议定义的属性都是在同一个类中实现的。
编辑:该死的我的阅读障碍(我的意思是粗心阅读)。 出于某种原因,我总是将“描述符”读为“装饰器”,反之亦然,即使我是同时输入这两者的人。 我将原封不动地保留我的帖子,因为它具有有效的信息,尽管信息与问题完全无关。
“装饰器”一词本身实际上是著名的《设计模式》一书中描述的一种设计模式的名称。 维基百科文章包含许多不同编程语言的装饰器用法示例: http://en.wikipedia.org/ wiki/Decorator_pattern
然而,那篇文章中的装饰器是面向对象的; 它们有实现预定义接口的类,该接口允许另一个现有类以某种方式表现不同,等等。Python 装饰器通过在运行时用另一个函数替换一个函数来以函数方式起作用,允许您有效地修改/替换该函数、插入代码等。
这在 Java 世界中称为面向方面编程,AspectJ Java 编译器可让您执行此类操作并将 AspectJ 代码(它是 Java 的超集)编译为 Java 字节码。
我对 C# 或 Ruby 不太熟悉,不知道他们的装饰器版本是什么。
Ruby and C# both easily let you create accessors by specifying getter/setter methods for an attribute, much like in Python. However, this isn't designed to naturally let you write the code for these methods in another class the way that Python allows. In practice, I'm not sure how much this matters, since every time I've seen an attribute defined through the descriptor protocol its been implemented in the same class.
EDIT: Darn my dyslexia (by which I mean careless reading). For some reason I've always read "descriptor" as "decorator" and vice versa, even when I'm the one typing both of them. I'll leave my post intact since it has valid information, albeit information which has absolutely nothing to do with the question.
The term "decorator" itself is actually the name of a design pattern described in the famous "Design Patterns" book. The Wikipedia article contains many examples in different programming languages of decorator usage: http://en.wikipedia.org/wiki/Decorator_pattern
However, the decorators in that article object-oriented; they have classes implementing a predefined interface which lets another existing class behave differently somehow, etc. Python decorators act in a functional way by replacing a function at runtime with another function, allowing you to effectively modify/replace that function, insert code, etc.
This is known in the Java world as Aspect-Oriented programming, and the AspectJ Java compiler lets you do these kinds of things and compile your AspectJ code (which is a superset of Java) into Java bytecode.
I'm not familiar enough with C# or Ruby to know what their version of decorators would be.
我也没有听说过直接等效的东西。 你也许可以用宏达到同样的效果,特别是在像 Lisp 这样具有极其强大的宏的语言中。
如果其他语言开始合并类似的东西,我一点也不会感到惊讶,因为它是如此强大。
I've not heard of a direct equivalent either. You could probably achieve the same effect with macros, especially in a language like Lisp which has extremely powerful macros.
I wouldn't be at all surprised if other languages start to incorporate something similar because it is so powerful.