search
HomeBackend DevelopmentPython Tutorialpython 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:

class CachedClassAttribute(CachedAttribute):

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()

>>> a.square

>>> 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)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you append elements to a Python list?How do you append elements to a Python list?May 04, 2025 am 12:17 AM

ToappendelementstoaPythonlist,usetheappend()methodforsingleelements,extend()formultipleelements,andinsert()forspecificpositions.1)Useappend()foraddingoneelementattheend.2)Useextend()toaddmultipleelementsefficiently.3)Useinsert()toaddanelementataspeci

How do you create a Python list? Give an example.How do you create a Python list? Give an example.May 04, 2025 am 12:16 AM

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

Discuss real-world use cases where efficient storage and processing of numerical data are critical.Discuss real-world use cases where efficient storage and processing of numerical data are critical.May 04, 2025 am 12:11 AM

In the fields of finance, scientific research, medical care and AI, it is crucial to efficiently store and process numerical data. 1) In finance, using memory mapped files and NumPy libraries can significantly improve data processing speed. 2) In the field of scientific research, HDF5 files are optimized for data storage and retrieval. 3) In medical care, database optimization technologies such as indexing and partitioning improve data query performance. 4) In AI, data sharding and distributed training accelerate model training. System performance and scalability can be significantly improved by choosing the right tools and technologies and weighing trade-offs between storage and processing speeds.

How do you create a Python array? Give an example.How do you create a Python array? Give an example.May 04, 2025 am 12:10 AM

Pythonarraysarecreatedusingthearraymodule,notbuilt-inlikelists.1)Importthearraymodule.2)Specifythetypecode,e.g.,'i'forintegers.3)Initializewithvalues.Arraysofferbettermemoryefficiencyforhomogeneousdatabutlessflexibilitythanlists.

What are some alternatives to using a shebang line to specify the Python interpreter?What are some alternatives to using a shebang line to specify the Python interpreter?May 04, 2025 am 12:07 AM

In addition to the shebang line, there are many ways to specify a Python interpreter: 1. Use python commands directly from the command line; 2. Use batch files or shell scripts; 3. Use build tools such as Make or CMake; 4. Use task runners such as Invoke. Each method has its advantages and disadvantages, and it is important to choose the method that suits the needs of the project.

How does the choice between lists and arrays impact the overall performance of a Python application dealing with large datasets?How does the choice between lists and arrays impact the overall performance of a Python application dealing with large datasets?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Explain how memory is allocated for lists versus arrays in Python.Explain how memory is allocated for lists versus arrays in Python.May 03, 2025 am 12:10 AM

InPython,listsusedynamicmemoryallocationwithover-allocation,whileNumPyarraysallocatefixedmemory.1)Listsallocatemorememorythanneededinitially,resizingwhennecessary.2)NumPyarraysallocateexactmemoryforelements,offeringpredictableusagebutlessflexibility.

How do you specify the data type of elements in a Python array?How do you specify the data type of elements in a Python array?May 03, 2025 am 12:06 AM

InPython, YouCansSpectHedatatYPeyFeLeMeReModelerErnSpAnT.1) UsenPyNeRnRump.1) UsenPyNeRp.DLOATP.PLOATM64, Formor PrecisconTrolatatypes.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft