Python descriptor (2), pythondescriptor

Source: Internet
Author: User
Tags setf

Python descriptor (2), pythondescriptor
Python built-in Descriptor

Python has some built-in descriptor objects, such as property, staticmethod, and classmethod. python implementation is as follows:

class Property(object):    def __init__(self,getf,setf,delf,doc):        self.getf=getf        self.setf=setf        self.delf=delf        self.doc=doc    def __set__(self,instance,own=None):        if instance is None:            return self        if  self.getf is None:            raise AttributeError        return self.getf(instance)    def __set__(self,instance,value):        if self.setf is None:            raise AttributeError        self.setf(instance,value)    def __del__(self,instance):        if self.delf is None:            raise AttributeError            self.delf(instance)class StaticMethod(object):    def __init__(self,func):        self.func=func    def __get__(self,instance,own=None):        return self.funcclass ClassMethod(object):    def __init__(self,func):        self.func=func    def __get__(self,instance,own=None):        if own is None:            own=type(instance)        def callfunc(*args):            return self.func(own,*args)        return callfunc
Set aliases for attribute values

Sometimes you want to use one property name as the alias of another property name. For example, if you want to set the default value of some properties, it must be the same as the current value of other properties, and you also need to set and delete them independently.

Class DefaultAlias (object): def _ init _ (self, name): self. name = name def _ get _ (self, instance, own): if instance is None: # return self return getattr (instance, self. name ). title () class Person (object): def _ init _ (self, name, aliasname = None): self. name = name if aliasname is not None: self. aliasname = DefaultAlias ('name ')
>>> p=Person('sam')>>> p.aliasname'Sam'>>> p.aliasname='jack'>>> p.aliasname'jack'>>> del p.aliasname>>> p.aliasname'Sam'

In this way, an alias aliasname is set for the attribute name, or the aliasname value is stored in the name. DefaultAlias is not a data descriptor because it does not have the _ set _ method, but rather a non-data descriptor. Therefore, when we assign a value to an instance property (p. aliasname = 'jack'), the instance will normally record the property, and the instance property will overwrite the class property. In this way, the aliasname attribute can be set separately without affecting the name attribute. When we delete del p. aliasname, the attributes of the instance are deleted and the class attributes are displayed again.

For some developed classes, to maintain compatibility with subsequent versions, you can use a new name to name methods and attributes, while retaining the availability of old names.

class OldAlias(object):    def __init__(self,name,oldname):        self.name=name        self.oldname=oldname    def _warn(self):        print 'use %r,not %r'%(self.name,self.oldname)    def __get__(self,instance,own):        self._warn()        if instance is None:              return self        return getattr(instance,self.name)    def __set__(self,instance,value):        self._warn()        setattr(instance,self.name,value)    def __del__(self,instance):        self._warn()        delattr(instance,self.name)class NewClass(object):    def __init__(self,newname):        self.newname=newname    oldname=OldAlias('newname','oldname')
>>> c=NewClass('a')>>> c.oldnameuse 'newname',not 'oldname''a'

The old code that uses this class uses the class attribute oldname, and a warning message is printed to encourage users to use the new attribute newname.

Cache property value

Calculate the values of instance attributes or class attributes as needed, and provide automatic cache.

class CachedAttribute(object):    def __init__(self,method,name=None):        self.method=method        self.name=name if name else method.__name__    def __get__(self,instance,own):        if instance is None:            return self        result=self.method(instance)        setattr(instance,self.name,result)        return resultclass MyObject(object):    def __init__(self,n):        self.n=n    @CachedAttribute    def square(self):        return self.n*self.nm
>>> m=MyObject(2)>>> m.square4>>> m.n=5>>> m.square4>>> del m.square>>> m.square25

After the first access to m. square, the square attribute is cached in instance m. When the instance attribute n is changed, the square attribute is not changed. If you need to clear the cache, del m. square. The value of accessing the square attribute again will be calculated again.

Cache class attributes:

class CachedClassAttribute(CachedAttribute):    def __get__(self,instance,own):        super(CachedClassAttribute,self).__get__(own,owna=)class MyClass(object):    class_attr=24    @CachedClassAttribute    def square(cls):        return cls.class_attr*cls.class_attr

All such instances have the same cache value:

>>> a=MyClass()>>> b=MyClass()>>> a.square>>> print a.square576>>> print b.square576>>> print MyClass.square576

  


Segment descriptor GDT LDT

Protection Mode (Protected Mode, or sometimes pmode) is a 80286 series and later x86 compatible CPU operation Mode. The protection mode has some new features and is designed to enhance the stability of multiple workers and systems, such as memory protection, paging systems, and virtual memory supported by hardware. Most of today's x86 operating systems are running in protected mode, including Linux, FreeBSD, and Microsoft Windows 2.0 and later versions.

The other 286 and later CPU operation modes are real, one is forward compatible and off these features. Designed to enable new chips to execute old software. According to the designed specifications, all x86 CPUs are started in real mode to ensure forward compatibility of traditional operating systems. Before any protection mode features available, they must be manually switched from some programs to the protection mode. In today's computers, such switching is usually the first task that must be completed by the operating system at startup. It may also use the virtual 86 mode to execute the program code designed for the real mode when the CPU runs in the protected mode. Although some software-based systems may use multiple jobs in real mode, the features of memory protection in protection mode are as follows, this prevents problematic programs from damaging the memory of other jobs or operating system cores. In the protection mode, hardware support for program interruption is also provided. execution content can be handed over to other jobs to implement preemptible multi-jobs. Most of the CPUs that can use the protection mode also have the characteristics of 32-Bit Memory (such as the 80386 series and any chips afterwards ), imported the integrated protection mode to become the 32-bit metadata processing concept. Although the 80286 chip supports the protection mode, it still has only 16-Bit Memory. Protection Mode enhancements in Windows 2.0 and later versions are called "386 enhancement mode" because they require 32-bit temporary memory in addition to the protection mode, and cannot be executed on 286 (even if 286 supports protection mode ). Even if the protection mode has been enabled on the 32-bit chip, the memory above 1 MB cannot be accessed, it is a memory wrap-around und (memory continuity) factor modeled on the features of the ibm xt system design. This restriction can be avoided by opening A20 line. In protection mode, the first 32 interrupts are reserved for CPU exception handling. For example, the interrupt 0D (decimal 13) is a normal protection mode error and the interrupt 00 is divided by zero. In the 8086/8088 era, there was only one operating Mode for the processor. At that time, this Mode was not named because there were no other operating modes. Since 80286 to 80386, the processor has added two more operating modes: protection Mode PM (Protected Mode) and System Management Mode SMM (System Management Mode). Therefore, 8086/8088 of the schema is named Real-address Mode RM (Real-address Mode ). PM is the native mode of the processor. In this mode, the processor supports all commands and all architecture features to provide the highest performance and compatibility. This mode is recommended for all new applications and operating systems. To ensure PM compatibility, the processor allows RM programs to be executed in a protected, multitasking environment. This feature is called Virtual-8086 Mode, although it is not a real processor Mode. The Virtual-8086 mode is actually a PM attribute, which can be used by any task. RM provides an Intel 8086 processor programming environment and some extensions (such as the ability to switch to PM or SMM ). When the host is powered-up or Reset, the processor is in RM.... The remaining full text>

How does python transmit file descriptors between processes?

Libancillary can do this (actually socket ):
Www.normalesup.org /~ George/comp/libancillary/

But it is C code, so you need some binding, such as ctypes:
Www.python.net/..l.html

I think you have asked me so deeply that I don't need to write a demonstration.

Related Article

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.