本文主要是介绍理解managed attribute||New-style Classes||descriptor in Python,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
-
Source
Description about
descriptor
in Python refersmanaged attribute
often.So, what the words
managed attribute
means. -
Chapter 38 《Learning Python, 5th Edition by Lutz, Mark》
Managed Attributes expands on the attribute interception techniques.
-
Why Manage Attributes
Normally, attributes are simply names for objects, a person’s
name
attribute, for example, might be a simple string, fetched and set with basic attribute syntax:person.name person.name = value
In most cases, the attribute lives in the object itself, or is inherited from a class from a class from which it derives.
-
前情回顾
managed attribute 是New-style Classes 的衍生概念。
Useful information on creating and using new-style classes:
- Unifying types and classes (aka descrintro) is Guido’s essay on new-style classes and should be your starting point.
- Raymond Hettinger’s How-To Guide for Descriptors focuses on the single most useful aspect of new-style classes (which includes properties).
- Python 2.3 Method Resolution Order (MRO) covers multiple inheritance.
- Metaclasses will make your head explode. Here are several approaches to discussing them:
- David Mertz and Michele Simionato’s DeveloperWorks article [1]
- Mike Fletcher’s slideshow
- Metaclasses in Five Minutes
- Types and Objects is the start of a new-style class tutorial with lots of figures and examples, but it’s rough and not complete.
-
Unifying types and classes in Python2.2
PEPs are not designed to be tutorials, and the PEPs(252, 253) describing the type/class unification are sometimes hard to read. That’s where this paper comes in: It introduces the key elements of the type/class unification for the average for the average Python programmer.
-
PEP 252 – Making Types Look More Like Classes
This PEP proposes changes to the introspection API for types that makes them look more like classes, and their instances more like class instances. For example, type(x) will be equivalent to
x.__class__
for most built-in types. WhenC
isx.__class__
,x.meth(a)
will generally be quivalent toC.meth(x, a)
, andC.__dict__
contains x’s methods and other attributes.This PEP also introduces a new approach to specifying attributes, using attribute descriptor, or descriptors for short.
Descriptors unify and generalize several different common mechanisms used for describing attributes: a descriptor can describe a method, a typed filed in the object structure, or a generalized attribute represented by getter and setter functions.
Based on the generalized
descriptor
API, this PEP also introduces a way to declareclass methods
andstatic methods
. -
Introduction
One of the Python’s oldest language warts is the difference between classes and types. For example, you can’t directly subclass the dictionary type, and the introspection interface for finding out what methods and instance variables an object has is different for types and for classes.
Healing the class/type split is a big effort, because it affects many aspects of how Python is implemented. This PEP concerns itself with making the introspection API for types look the same as that for classes. Other PEPs will propose making classes look more like types, and subclassing from built-in types; these topics are not on the table for this PEP.
-
Introspection APIs
Introspection concerns itself with finding out what attributes an object has. Python’s very general getattr/setattr API makes it impossible to guarantee that there always is a way to get a list of all attributes supported by a specific object, but in practice two conventions have appreared that together work for almost all objects. I’ll call them the class-based introspection API and the type-based introspection API; class API and type API for short.
这篇关于理解managed attribute||New-style Classes||descriptor in Python的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!