Python 从哪里借用了描述符的概念? [英] Where did Python borrow the concept of descriptor from?

查看:42
本文介绍了Python 从哪里借用了描述符的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 从哪里借用了 描述符 的概念?

Where did Python borrow the concept of descriptor from?

Python 是否从其他一些编程语言中借用了它?在 C# 中,我只看到属性,而看不到描述符.

Did Python borrow it from some other programming languages? In C#, I only see property, but not descriptor.

通过先学习其他语言而不是直接学习 Python,我不仅会受益于描述符,还会受益于其他概念吗?

Will I benefit on not just descriptors but other concepts by learning that other language first, instead of learning Python directly?

推荐答案

来自 Python 本身.引用 Guido Van Rossum 1描述符是在 Python 2.2 中引入的,作为绑定方法概念的概括,这是以前版本中经典类实现的核心.

From Python itself. To quote Guido Van Rossum 1, descriptors were introduced in Python 2.2 as a generalization of the concept of bound methods, which was central to the implementation of classic classes in previous versions.

一种语言有两种常见的方法来支持函数式编程和面向对象编程.每个 callable 都是一个方法,函数只是一个隐藏类的方法,这是 Ruby 采用的方法;或者每个可调用对象都是一个函数,而对象方法只是带有隐式或显式参数的函数,用于接收调用方法的对象,这是 Python 采用的方法.

There are two common approaches for a language to support both functional and object oriented programming. Every callable is a method and functions are just methods of a hidden class, which is the approach adopted by Ruby; or every callable is a function and object methods are just functions with an implicit or explicit parameter that receives the object the method was called from, which is the approach adopted by Python.

在 Python 2.1 及更早版本中,当在实例字典中查找实例属性失败时,解释器会递归地查看类和基类.如果在类字典中找到该属性并且它是一个函数,则解释器将其包装在一个可调用对象中,该对象在调用时将实例本身作为第一个参数插入.包装器将实例绑定到函数,因此称为绑定方法.

In Python 2.1 and before, when an instance attribute lookup fails at the instance dictionary, the interpreter looks at the class and the base classes, recursively. If the attribute is found at a class dictionary and it's a function, the interpreter wraps it in a callable object that inserts the instance itself as the first parameter when it gets called. The wrapper binds the instance to the function, hence the name bound methods.

描述符是该行为的概括.在 Python 2.2 及更高版本中,解释器以相同的方式执行属性查找,当在类字典中找到该属性并且它有一个 __get__ 方法时,该方法会被类和实例调用作为参数,返回的任何内容都用作属性搜索的结果.绑定方法包装器不再是一个特殊的对象,现在只是一个实现 __get__ 方法的普通对象,允许我们自定义它并实现诸如 classmethod 之类的特殊方法>静态方法.最重要的是,我们有互补的方法 __set____delete__,概括了相同的赋值和删除行为,允许我们实现诸如 property 和管理的各种属性.

Descriptors are a generalization of that behavior. In Python 2.2 and later, the interpreter performs the attribute lookup in the same way, and when the attribute is found at a class dictionary and it has a __get__ method, this method gets called with the class and instance as arguments, and whatever is returned is used as the result of the attribute search. Instead of being a special object, the bound method wrapper is now just an ordinary object implementing a __get__ method, allowing us to customize it and implement special methods like classmethod and staticmethod. On top of that, we have the complementary methods __set__ and __delete__, generalizing the same behavior for assignment and deletion, allowing us to implement things like property and managed attributes of all kinds.

这篇关于Python 从哪里借用了描述符的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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