Some knowledge questions about descriptor in Python

Source: Internet
Author: User

The problem starts with the morning routine sweep Segmentfault.

There's a problem.

class C (object):    @classmethod    def  m ():        passm () is a class method with the calling code as follows: C.M () But I want to call it as a property, like this: C.M, how can I get it? Please provide a simple example, thank you!

Here I began to misunderstand what he meant, thinking that he was going to use C () directly. M call this method, if so, directly to the adorner @classmathod change to @property can achieve the effect.

But the effect he wants to achieve here is C.M, which means that if you don't instantiate a C object, calling the M method is a bit like using classmathod and then calling the property method on that basis.

I thought for a long time, did not have encountered in the previous code, therefore came the interest.

After extensive searching for information, I found it was related to a concept called Descriptor protocol.

What is descriptor? An introduction to the official documentation is:

In general, a descriptor are an object attribute with "binding behavior", one whose attribute access have been overridden by Methods in the Descriptor protocol. Those methods is __get__() , __set__() , and __delete__() . If any of those methods was defined for a object, it's said to BES a descriptor

Typically, a descriptor is an object of binding behavior, and a property is overridden by a method in the description protocol. Those methods include __get__ (), __set__ () and __delete__ (). If any of the above methods appear in the object you define, then we say that he is a descriptor.

In other words, if you define a class method, and then the class method contains any of the __get__,__set__ and __delete__ methods, then it is a descriptor.

Believe that most people are using the above mentioned property method, but do not know if there is no pot friends carefully read the property source, in fact, the property is a typical descriptor.

So much to say and not understand to reproduce the above problem how to solve

classClasspro (object):def __init__(self, function): Self.run=functiondef __get__(self, instance, owner):returnSelf.run (owner)classA (object):defpp (self):Print 'Say something'pp=Classpro (PP) a.pp
Output:say something

As you can see above, I implemented a custom new class Classpro to decorate the function pp inside the Class A below. CP implements the __get__ method so he is a descriptor,descriptor once implemented, it overrides the original way of looking for attributes from the __dict__. Here I quote the translation of the doc from someone else's blog.

Typically, access to an instance's properties, such as GET, set, delete, is done through the instance's __dict__ dictionary property, for example, for Operation A.x, a lookup chain from a.__dict[' X '] (the Dictionary of Instances), and then to type (a). __ dict__[' x '] (Dictionary of classes), to the dictionary of the parent class of type (a), and so on.
If an object defines the __get__,__set__ method at the same time, it is considered to be data descriptor; only __get__ is defined, known as Non-data descriptor. If there is a key in the instance dictionary with the same name as data descriptor, then the data descriptor is preferred when searching, and the instance dictionary is preferred if there is a key and Non-data descriptor in the instance dictionary with the same name.

That's probably it.

Preference:

Http://stackoverflow.com/questions/17330160/how-does-the-property-decorator-work How-does-the-property-decorator-work

Some concepts based on descriptor in http://www.cnblogs.com/btchenguang/archive/2012/09/17/2689146.html python (above)

Some concepts based on descriptor in http://www.cnblogs.com/btchenguang/archive/2012/09/18/2690802.html#3416935 python (bottom)

Https://docs.python.org/2.7/howto/descriptor.html Descriptor HOWTO Guide

Http://stackoverflow.com/questions/128573/using-property-on-classmethods using Property () on Classmethods

Some knowledge questions about descriptor in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.