- 浏览: 36619 次
-
最新评论
-
诗意的栖居:
我按照你给的地址下载了,安装了,但是mysql却不能启动:_m ...
MySQLdb for python 安装 -
诗意的栖居:
多谢,还是这样比较省事
MySQLdb for python 安装
How-To Guide for Descriptors
Author: | Raymond Hettinger |
Contact: | <python at rcn dot com> |
Copyright: | Copyright (c) 2003, 2004 Python Software Foundation.All rights reserved. |
Contents
Abstract
Defines descriptors, summarizes the protocol, and shows how descriptors arecalled. Examines a custom descriptor and several built-in python descriptorsincluding functions, properties, static methods, and class methods. Shows howeach works by giving a pure Python equivalent and a sample application.
Learning about descriptors not only provides access to a larger toolset, itcreates a deeper understanding of how Python works and an appreciation for theelegance of its design.
Definition and Introduction
In general, a descriptor is an object attribute with "binding behavior", one whoseattribute access has been overridden by methods in the descriptor protocol. Thosemethods are__get__ , __set__ , and __delete__ . If any of those methods aredefined for an object, it is said to be a descriptor.
The default behavior for attribute access is to get, set, or delete theattribute from an object's dictionary. For instance,a.x has a lookup chainstarting with a.__dict__['x'] , then type(a).__dict__['x'] , and continuingthrough the base classes oftype(a) excluding metaclasses.If the looked-up value is an object defining one of thedescriptor methods, then Python may override the default behavior and invoke thedescriptor method instead. Where this occurs in the precedence chain depends onwhich descriptor methods were defined. Note that descriptors are only invokedfor new style objects or classes (a class is new style if it inherits fromobject ortype ).
Descriptors are a powerful, general purpose protocol. They are the mechanismbehind properties, methods, static methods, class methods, andsuper() .They are used used throughout Python itself to implement the new style classesintroduced in version 2.2. Descriptors simplify the underlying C-code and offera flexible set of new tools for everyday Python programs.
Descriptor Protocol
descr.__get__(self, obj, type=None) --> value
descr.__set__(self, obj, value) --> None
descr.__delete__(self, obj) --> None
That is all there is to it. Define any of these methods and an object isconsidered a descriptor and can override default behavior upon being looked upas an attribute.
If an object defines both __get__ and __set__ , it is considered a datadescriptor. Descriptors that only define __get__ are called non-datadescriptors (they are typically used for methods but other uses are possible).
Data and non-data descriptors differ in how overrides are calculated withrespect to entries in an instance's dictionary. If an instance's dictionaryhas an entry with the same name as a data descriptor, the data descriptortakes precedence. If an instance's dictionary has an entry with the samename as a non-data descriptor, the dictionary entry takes precedence.
To make a read-only data descriptor, define both __get__ and__set__ with the __set__ raising anAttributeError when called. Defining the__set__ method with an exception raising placeholder is enough to makeit a data descriptor.
Invoking Descriptors
A descriptor can be called directly by its method name. For example,d.__get__(obj) .
Alternatively, it is more common for a descriptor to be invoked automaticallyupon attribute access. For example,obj.d looks up d in the dictionaryofobj . If d defines the method __get__ , then d.__get__(obj) is invoked according to the precedence rules listed below.
The details of invocation depend on whether obj is an object or a class. Eitherway, descriptors only work for new style objects and classes. A class is newstyle if it is a subclass ofobject .
For objects, the machinery is in object.__getattribute__ which transformsb.x intotype(b).__dict__['x'].__get__(b, type(b)) . The implementation worksthrough a precedence chain that gives data descriptors priority over instancevariables, instance variables priority over non-data descriptors, and assignslowest priority to __getattr__ if provided. The full C implementation can befound inPyObject_GenericGetAttr() in Objects/object.c .
For classes, the machinery is in type.__getattribute__ which transformsB.x into B.__dict__['x'].__get__(None, B) . In pure Python, it looks like:
The important points to remember are:
- descriptors are invoked by the __getattribute__ method
- overriding __getattribute__ prevents automatic descriptor calls
- __getattribute__ is only available with new style classes and objects
- object.__getattribute__ and type.__getattribute__ make differentcalls to__get__ .
- data descriptors always override instance dictionaries.
- non-data descriptors may be overridden by instance dictionaries.
The object returned by super() also has a custom __getattribute__ method for invoking descriptors. The call super(B, obj).m() searchesobj.__class__.__mro__ for the base classA immediately following B and then returnsA.__dict__['m'].__get__(obj, A) . If not a descriptor,m is returned unchanged. If not in the dictionary,m reverts to asearch using object.__getattribute__ .
Note, in Python 2.2, super(B, obj).m() would only invoke__get__ ifm was a data descriptor. In Python 2.3, non-data descriptors also getinvoked unless an old-style class is involved. The implementation detailsare insuper_getattro() in Objects/typeobject.c and a pure Pythonequivalent can be found inGuido's Tutorial .
The details above show that the mechanism for descriptors is embedded in the__getattribute__() methods forobject , type , and super . Classesinherit this machinery when they derive from object or if they have ameta-class providing similar functionality. Likewise, classes can turn-offdescriptor invocation by overriding__getattribute__() .
Descriptor Example
The following code creates a class whose objects are data descriptors whichprint a message for each get or set. Overriding__getattribute__ isalternate approach that could do this for every attribute. However, thisdescriptor is useful for monitoring just a few chosen attributes:
The protocol is simple and offers exciting possibilities. Several use cases areso common that they have been packaged into individual function calls.Properties, bound and unbound methods, static methods, and class methods are allbased on the descriptor protocol.
Properties
Calling property() is a succinct way of building a data descriptor that triggersfunction calls upon access to an attribute. Its signature is:
property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
The documentation shows a typical use to define a managed attribute x :
To see how property() is implemented in terms of the descriptor protocol, hereis a pure Python equivalent:
The property() builtin helps whenever a user interface has granted attributeaccess and then subsequent changes require the intervention of a method.
For instance, a spreadsheet class may grant access to a cell value throughCell('b10').value . Subsequent improvements to the program require the cell to berecalculated on every access; however, the programmer does not want to affectexisting client code accessing the attribute directly. The solution is to wrapaccess to the value attribute in aproperty() data descriptor:
Functions and Methods
Python's object oriented features are built upon a function based environment.Using non-data descriptors, the two are merged seamlessly.
Class dictionaries store methods as functions. In a class definition, methodsare written usingdef and lambda , the usual tools for creatingfunctions. The only difference from regular functions is that the firstargument is reserved for the object instance. By Python convention, theinstance reference is called self but may be called this or anyother variable name.
To support method calls, functions include the __get__ method for bindingmethods during attribute access. This means that all functions are non-datadescriptors which return bound or unbound methods depending whether they areinvoked from an object or a class. In pure python, it works like this:
Running the interpreter shows how the function descriptor works in practice:
The output suggests that bound and unbound methods are two different types.While they could have been implemented that way, the actual C implemention ofPyMethod_Type inObjects/classobject.c is a single object with two differentrepresentations depending on whether theim_self field is set or is NULL (the Cequivalent ofNone ).
Likewise, the effects of calling a method object depend on the im_self field.If set (meaning bound), the original function (stored in the im_func field) iscalled as expected with the first argument set to the instance. If unbound, allof the arguments are passed unchanged to the original function. The actual Cimplementation ofinstancemethod_call() is only slightly more complex in that itincludes some type checking.
Static Methods and Class Methods
Non-data descriptors provide a simple mechanism for variations on the usualpatterns of binding functions into methods.
To recap, functions have a __get__ method so that they can be converted to amethod when accessed as attributes. The non-data descriptor transforms aobj.f(*args) call intof(obj, *args) . Calling klass.f(*args) becomesf(*args) .
This chart summarizes the binding and its two most useful variants:
Transformation Called from anObject Called from aClass Descriptor function f(obj, *args) f(*args) staticmethod f(*args) f(*args) classmethod f(type(obj), *args) f(klass, *args)
Static methods return the underlying function without changes. Calling eitherc.f orC.f is the equivalent of a direct lookup intoobject.__getattribute__(c, "f") orobject.__getattribute__(C, "f") .As a result, the function becomes identically accessible from either an object or a class.
Good candidates for static methods are methods that do not reference theself variable.
For instance, a statistics package may include a container class forexperimental data. The class provides normal methods for computing the average,mean, median, and other descriptive statistics that depend on the data.However, there may be useful functions which are conceptually related but do notdepend on the data. For instance, erf(x) is handy conversion routine that comesup in statistical work but does not directly depend on a particular data set.It can be called either from an object or the class:s.erf(1.5) --> .9332 orSample.erf(1.5) --> .9332 .
Since staticmethods return the underlying function with no changes, the examplecalls are unexciting:
>>> class E(object): def f(x): print x f = staticmethod(f) >>> print E.f(3) 3 >>> print E().f(3) 3
Using the non-data descriptor protocol, a pure Python version of staticmethod() would look like this:
Unlike static methods, class methods prepend the class reference to theargument list before calling the function. This format is the samefor whether the caller is an object or a class:
This behavior is useful whenever the function only needs to have a classreference and does not care about any underlying data. One use forclassmethods is to create alternate class constructors. In Python 2.3,the classmethoddict.fromkeys() creates a new dictionary from a listof keys. The pure Python equivalent is:
Now a new dictionary of unique keys can be constructed like this:
>>> Dict.fromkeys('abracadabra') {'a': None, 'r': None, 'b': None, 'c': None, 'd': None}
Using the non-data descriptor protocol, a pure Python version ofclassmethod() would look like this:
转载自:http://users.rcn.com/python/download/Descriptor.htm
发表评论
-
python 的函数Decorators
2010-03-05 16:41 661Decorators是python中比较难以理解的东西,当然如 ... -
Python Unicode与中文处理
2010-03-19 15:06 945python中的unicode是让人很 ... -
Python中的正则表达式匹配中文问题
2010-03-23 15:53 992python中正则表达式匹配中文是没有问题的,但是其中有一个关 ... -
MySQLdb for Python使用指南/Python的数据库操作
2010-03-24 13:19 824转载自:http://hi.baidu.com ... -
python MySQLdb示例代码
2010-03-24 13:25 668# -*- coding: utf-8 -*- imp ... -
Python打包、安装与发布工具--setuptools
2010-04-01 17:10 1712Python中的setuptools工具不仅仅是简化了dist ... -
有道难题 之 有道搜索框 java实现
2010-06-02 16:51 820这个题目是5.29号第一场的第二题,先说句题外话,众多的参加的 ... -
CentOS5.4上将Python版本升级到2.6.5
2010-08-12 16:07 948CentOS5.4上将Python版本升级到2.6.5 ... -
MySQLdb for python 安装
2011-04-23 14:12 925升级到了python2.7.1,没 ... -
python lib 之 operator
2011-05-06 16:29 931operator module在使用内置函数如map,iter ... -
python mysql 分页程序
2011-10-09 16:00 3620#!/usr/bin/env python # -*- co ... -
python __getattribute__ 的优先级问题
2011-10-26 14:54 1976The way __getattribute__ ... -
Thundering Herd Mitigation (memcached redis)
2012-04-25 16:38 1141"Modified memcached cache ... -
python 算法
2011-10-21 17:14 601http://code.activestate.com/rec ...
相关推荐
This is a sad state of affairs, as it makes it difficult for Python developers to get a really good understanding of how descriptors work and the techniques to avoid the big gotchas associated with ...
- **Selecting Input from a Serial Port**: Using the `select()` system call to monitor multiple file descriptors for input readiness. This is useful for non-blocking I/O operations and handling ...
With this hands-on guide, you’ll learn how to write effective, idiomatic Python code by leveraging its best—and possibly most neglected—features. Author Luciano Ramalho takes you through Python’s...
The book "XDoclet in Action" provides a comprehensive guide on how to integrate XDoclet into the development process, offering practical examples that cover various aspects of Java technology. ...
Practical and precise, this hands-on guide with ready answers is designed for IT professionals working with Microsoft products, whether for on-premises, hybrid or cloud support. Inside, you'll find ...
MS Windows 3.0 SDK Guide to Programming MS Windows 3.0 SDK Install. & Update Guide MS Windows 3.0 SDK Programmer's Reference Vol. 1 MS Windows 3.0 SDK Programmer's Reference Vol. 2 MS Windows 3.0...
The Linux Kernel 2.4 Internals provide a comprehensive guide to understanding the inner workings of the kernel. Each section delves deep into specific aspects of the kernel's functionality, making it ...
A Brief Guide to Calling API Functions 281 Managed and Unmanaged Code 282 Documentation 284 Using Visual C++ .NET 284 Using Visual Basic .NET 286 Finding Your Device 291 Obtaining the Device Interface...
A guide to how the FreeBSD kernel manages the IA32 processors in Protected Mode (c) 2004, Arne Vidstrom, http://vidstrom.net Version 1.0 : 2004-06-17 Table of contents 1. P REREQUISITE KNOWLEDGE ........