python descriptor descriptor(2)
python内置的描述符
python有些内置的描述符对象,PRoperty、staticmethod、classmethod,python实现如下:
class Property(object):
def __init__(self,getf,setf,delf,doc):
self.getf=getf
self.setf=setf
self.delf=delf
self.doc=doc
def __get__(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.func
class 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
为属性值设置别名
有时候你想用一个属性名作为另一个属性名的别名,比如设置一些属性的默认值必须和其他属性的当前值一样,而且还需要独立的设置和删除。
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=aliasname
aliasname=DefaultAlias('name')
>>> p=Person('sam')
>>> p.aliasname
'Sam'
>>> p.aliasname='jack'
>>> p.aliasname
'jack'
>>> del p.aliasname
>>> p.aliasname
'Sam'
这样就为属性name设置了一个别名aliasname,或者说把aliasname的值存储在了name中。DefaultAlias并不是数据描述符,因为它没有__set__方法,而是一个non-data描述符。所以我们给一个实例属性赋值时(p.aliasname='jack'),实例会正常地记录属性,而且实例属性会覆盖掉类属性。这样aliasname属性就能单独的设置而不影响name属性了。当我们del p.aliasname时,删除了实例的属性,类属性又会再次显现出来。
对于某些开发的类,如果要保持后续版本的兼容性,可以用新名称来命名方法和属性,同时保留旧名字的可用性。
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 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.oldname
use 'newname',not 'oldname'
'a'
Old code using this class will use the class attribute oldname, and a warning message will be printed to encourage the user to use the new attribute newname.
Cache attribute values
Calculate the value of instance attributes or class attributes based on requirements and provide automated caching.
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): C iF Instance is None:
Return Self
Result = Self.method (Instance)
Setattr (Instance, Self.name, Result) Return Result
Class MyObject (Object): I DEF __init __ (Self, N):
Self .n=n
@CachedAttribute
def square(self):
return self.n*self.n
>>> m=MyObject(2)
>>> m.square
4
> ;>> m.n=5
>>> m.square
4
>>> del m.square
>>> m.square
25
on first visit m After .square, the square attribute is cached in instance m. When the instance attribute n is changed, the square attribute will not change. If you need to clear the cache, just del m.square. If you access the m.square attribute square again, the value of square will be calculated again.
Cache class attributes:
def __get__(self,instance,own):
return super(CachedClassAttribute,self).__get__(own,own)class MyClass(object):
class_attr= 24 @CachedClassAttribute
def square(cls):
return cls.class_attr*cls.class_attr
All instances of this class have the same cached value:
>>> a=MyClass()
>>> b=MyClass()
>>> print a.square
576>>> print b.square
576
> >> print MyClass.square
576
The above is the content of the python descriptor descriptor (2). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
