소개
설명자(descriptor)는 Python 언어에서 심오하면서도 중요한 마법입니다. Python 언어의 커널에서 널리 사용됩니다. , 설명자를 마스터하면 Python프로그래머의 도구 상자에 추가 기술이 추가됩니다. 이 기사에서는 설명자의 정의와 몇 가지 일반적인 시나리오에 대해 설명하고 기사 마지막에 getattr
, getattribute
, getitem
세 가지 magic method 를 추가하겠습니다. 여기에는 속성 액세스도 포함됩니다 >.
설명자 정의
descrget(self, obj, objtype=None) --> value descr.set(self, obj, value) --> None descr.delete(self, obj) --> None
단 하나의 <code><a href="http://www.php.cn/wiki/60.html" target="_blank">object</a> attribute
object 속성(Object
RevealAcess
다음 예에서는 get
클래스를 만들고
class RevealAccess(object): def get(self, obj, objtype): print('self in RevealAccess: {}'.format(self)) print('self: {}\nobj: {}\nobjtype: {}'.format(self, obj, objtype)) class MyClass(object): x = RevealAccess() def test(self): print('self in MyClass: {}'.format(self))EX1 인스턴스 속성
get
다음으로 self
메소드의 각 매개변수의 의미를 살펴보겠습니다. 다음 예에서 obj
는 RevealAccess 클래스 인스턴스 x, objtype
는 MyClass 클래스의 인스턴스 m이고, m.x
는 이름에서 알 수 있듯이 MyClass 클래스 자체입니다. 출력 문에서 볼 수 있듯이 x
액세스 설명자 get
는
>>> m = MyClass() >>> m.test() self in MyClass: <main.MyClass object at 0x7f19d4e42160> >>> m.x self in RevealAccess: <main.RevealAccess object at 0x7f19d4e420f0> self: <main.RevealAccess object at 0x7f19d4e420f0> obj: <main.MyClass object at 0x7f19d4e42160> objtype: <class 'main.MyClass'>EX2 클래스 속성
x
obj
속성이 클래스를 통해 직접 액세스되는 경우
>>> MyClass.x
self in RevealAccess: <main.RevealAccess object at 0x7f53651070f0>
self: <main.RevealAccess object at 0x7f53651070f0>
obj: None
objtype: <class 'main.MyClass'>
설명자 원리설명자 트리거위의 예에서는 각각 인스턴스 속성과 클래스 속성의 관점에서 설명자의 사용법을 나열했습니다. 내부 원리 분석: 实例属性
type(obj).dict['d'].get(obj, type(obj))
에 액세스하면 기본 클래스 객체의 getattribute 메서드가 실제로 호출되고, 이 메서드에서 obj.d가 번역됩니다. >.类属性
에 액세스하면 cls.d를cls.dict['d'].get(None, cls)
로 변환하는 메타클래스 유형의 getattribute 메서드를 호출하는 것과 같습니다. 여기서 get()의 obj는 None입니다. 인스턴스가 존재하지 않기 때문입니다.
getattribute
매직 메소드에 대해 간단히 이야기해보겠습니다. 이 메소드는 객체의 속성에 접근할 때 무조건 호출됩니다. 자세한 내용은 getattr
, getitem
과 같습니다. 기사 마지막 부분에 추가 보충 자료를 작성하겠지만 지금은 자세히 다루지 않겠습니다.
설명자우선순위
우선, 설명자는 두 가지 유형으로 나뉩니다.
객체가 get( ) 및 set() 메소드를 사용하는 경우 이 설명자를
data descriptor
이라고 합니다.객체가 get() 메서드만 정의하는 경우 이 설명자를
non-data descriptor
이라고 합니다.
속성에 액세스할 때 네 가지 상황이 있습니다.
데이터 설명자
인스턴스 사전
비데이터 설명자
getattr()
우선순위 크기는
data descriptor > instance dict > non-data descriptor > getattr()
이게 무슨 뜻인가요? 즉, 동일한 이름을 가진 data descriptor->d
과 instance attribute->d
가 인스턴스 객체 obj에 나타나면 obj.d
가 d
속성에 액세스할 때 데이터 설명자의 우선순위가 더 높기 때문에 Python은 type(obj).dict['d'].get(obj, type(obj))
을 호출합니다. 대신 obj.dict['d']를 호출하지 않습니다. 그러나 설명자가 데이터 설명자가 아닌 경우 Python은 obj.dict['d']
을 호출합니다.
속성
설명자를 사용할 때마다 설명자 클래스를 정의하는 것은 매우 번거로운 작업 같습니다. Python은 속성에 데이터 설명자를 추가하는 간결한 방법을 제공합니다.
property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
fget, fset 및 fdel은 각각 클래스의 getter, setter 및 deleter 메서드입니다. 다음 예를 사용하여 속성 사용 방법을 설명합니다.
class Account(object): def init(self): self._acct_num = None def get_acct_num(self): return self._acct_num def set_acct_num(self, value): self._acct_num = value def del_acct_num(self): del self._acct_num acct_num = property(get_acct_num, set_acct_num, del_acct_num, '_acct_num property.')
acct가 Account의 인스턴스인 경우 acct.acct_num은 getter를 호출하고 acct.acct_num = value는 setter를 호출하며 del acct_num.acct_num 삭제자를 호출합니다.
>>> acct = Account() >>> acct.acct_num = 1000 >>> acct.acct_num 1000
Python은 간단한 애플리케이션 시나리오를 위한 속성을 생성하는 데 사용할 수 있는 @property
데코레이터도 제공합니다. 속성 객체에는 getter, setter 및 delete 데코레이터 메서드가 있으며, 이는 해당 장식된 함수 의 접근자 함수를 통해 속성의 복사본을 만드는 데 사용할 수 있습니다.
class Account(object): def init(self): self._acct_num = None @property # the _acct_num property. the decorator creates a read-only property def acct_num(self): return self._acct_num @acct_num.setter # the _acct_num property setter makes the property writeable def set_acct_num(self, value): self._acct_num = value @acct_num.deleter def del_acct_num(self): del self._acct_num
속성을 읽기 전용으로 설정하려면 setter 메소드를 제거하면 됩니다.
在运行时创建描述符
我们可以在运行时添加property属性:
class Person(object): def addProperty(self, attribute): # create local setter and getter with a particular attribute name getter = lambda self: self._getProperty(attribute) setter = lambda self, value: self._setProperty(attribute, value) # construct property attribute and add it to the class setattr(self.class, attribute, property(fget=getter, \ fset=setter, \ doc="Auto-generated method")) def _setProperty(self, attribute, value): print("Setting: {} = {}".format(attribute, value)) setattr(self, '_' + attribute, value.title()) def _getProperty(self, attribute): print("Getting: {}".format(attribute)) return getattr(self, '_' + attribute)
>>> user = Person() >>> user.addProperty('name') >>> user.addProperty('phone') >>> user.name = 'john smith' Setting: name = john smith >>> user.phone = '12345' Setting: phone = 12345 >>> user.name Getting: name 'John Smith' >>> user.dict {'_phone': '12345', '_name': 'John Smith'}
静态方法和类方法
我们可以使用描述符来模拟Python中的@<a href="http://www.php.cn/wiki/188.html" target="_blank">static</a>method
和@classmethod
的实现。我们首先来浏览一下下面这张表:
Transformation | Called from an Object | Called from a Class |
---|---|---|
function | f(obj, *args) | f(*args) |
staticmethod | f(*args) | f(*args) |
classmethod | f(type(obj), *args) | f(klass, *args) |
静态方法
对于静态方法f
。c.f
和C.f
是等价的,都是直接查询object.getattribute(c, ‘f’)
或者object.getattribute(C, ’f‘)
。静态方法一个明显的特征就是没有self
变量。
静态方法有什么用呢?假设有一个处理专门数据的容器类,它提供了一些方法来求平均数,中位数等统计数据方式,这些方法都是要依赖于相应的数据的。但是类中可能还有一些方法,并不依赖这些数据,这个时候我们可以将这些方法声明为静态方法,同时这也可以提高代码的可读性。
使用非数据描述符来模拟一下静态方法的实现:
class StaticMethod(object): def init(self, f): self.f = f def get(self, obj, objtype=None): return self.f
我们来应用一下:
class MyClass(object): @StaticMethod def get_x(x): return x print(MyClass.get_x(100)) # output: 100
类方法
Python的@classmethod
和@staticmethod
的用法有些类似,但是还是有些不同,当某些方法只需要得到类的<a href="http://www.php.cn/wiki/231.html" target="_blank">引用</a>
而不关心类中的相应的数据的时候就需要使用classmethod了。
使用非数据描述符来模拟一下类方法的实现:
class ClassMethod(object): def init(self, f): self.f = f def get(self, obj, klass=None): if klass is None: klass = type(obj) def newfunc(*args): return self.f(klass, *args) return newfunc
其他的魔术方法
首次接触Python魔术方法的时候,我也被get
, getattribute
, getattr
, getitem
之间的区别困扰到了,它们都是和属性访问相关的魔术方法,其中重写getattr
,getitem
来构造一个自己的集合类非常的常用,下面我们就通过一些例子来看一下它们的应用。
getattr
Python默认访问类/实例的某个属性都是通过getattribute
来调用的,getattribute
会被无条件调用,没有找到的话就会调用getattr
。如果我们要定制某个类,通常情况下我们不应该重写getattribute
,而是应该重写getattr
,很少看见重写getattribute
的情况。
从下面的输出可以看出,当一个属性通过getattribute
无法找到的时候会调用getattr
。
In [1]: class Test(object): ...: def getattribute(self, item): ...: print('call getattribute') ...: return super(Test, self).getattribute(item) ...: def getattr(self, item): ...: return 'call getattr' ...: In [2]: Test().a call getattribute Out[2]: 'call getattr'
应用
对于默认的字典,Python只支持以obj['foo']
形式来访问,不支持obj.foo
的形式,我们可以通过重写getattr
让字典也支持obj['foo']
的访问形式,这是一个非常经典常用的用法:
class Storage(dict): """ A Storage object is like a dictionary except `obj.foo` can be used in addition to `obj['foo']`. """ def getattr(self, key): try: return self[key] except KeyError as k: raise AttributeError(k) def setattr(self, key, value): self[key] = value def delattr(self, key): try: del self[key] except KeyError as k: raise AttributeError(k) def repr(self): return '<Storage ' + dict.repr(self) + '>'
我们来使用一下我们自定义的加强版字典:
>>> s = Storage(a=1) >>> s['a'] 1 >>> s.a 1 >>> s.a = 2 >>> s['a'] 2 >>> del s.a >>> s.a ... AttributeError: 'a'
getitem
getitem用于通过下标[]
的形式来获取对象中的元素,下面我们通过重写getitem
来实现一个自己的list。
class MyList(object): def init(self, *args): self.numbers = args def getitem(self, item): return self.numbers[item] my_list = MyList(1, 2, 3, 4, 6, 5, 3) print my_list[2]
这个实现非常的简陋,不支持slice和step等功能,请读者自行改进,这里我就不重复了。
应用
下面是参考requests库中对于getitem
的一个使用,我们定制了一个忽略属性大小写的字典类。
程序有些复杂,我稍微解释一下:由于这里比较简单,没有使用描述符的需求,所以使用了@property
装饰器来代替,lower_keys
的功能是将实例字典
中的键全部转换成小写并且存储在字典self._lower_keys
中。重写了getitem
方法,以后我们访问某个属性首先会将键转换为小写的方式,然后并不会直接访问实例字典,而是会访问字典self._lower_keys
去查找。赋值/删除操作的时候由于实例字典会进行变更,为了保持self._lower_keys
和实例字典同步,首先清除self._lower_keys
的内容,以后我们重新查找键的时候再调用getitem
的时候会重新新建一个self._lower_keys
。
class CaseInsensitiveDict(dict): @property def lower_keys(self): if not hasattr(self, '_lower_keys') or not self._lower_keys: self._lower_keys = dict((k.lower(), k) for k in self.keys()) return self._lower_keys def _clear_lower_keys(self): if hasattr(self, '_lower_keys'): self._lower_keys.clear() def contains(self, key): return key.lower() in self.lower_keys def getitem(self, key): if key in self: return dict.getitem(self, self.lower_keys[key.lower()]) def setitem(self, key, value): dict.setitem(self, key, value) self._clear_lower_keys() def delitem(self, key): dict.delitem(self, key) self._lower_keys.clear() def get(self, key, default=None): if key in self: return self[key] else: return default
我们来调用一下这个类:
>>> d = CaseInsensitiveDict() >>> d['ziwenxie'] = 'ziwenxie' >>> d['ZiWenXie'] = 'ZiWenXie' >>> print(d) {'ZiWenXie': 'ziwenxie', 'ziwenxie': 'ziwenxie'} >>> print(d['ziwenxie']) ziwenxie # d['ZiWenXie'] => d['ziwenxie'] >>> print(d['ZiWenXie']) ziwenxi
위 내용은 Python 흑마법 설명자 사용 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

Arraysinpython, 특히 비밀 복구를위한 ArecrucialInscientificcomputing.1) theaRearedFornumericalOperations, DataAnalysis 및 MachinELearning.2) Numpy'SimplementationIncensuressuressurations thanpythonlists.3) arraysenablequick

Pyenv, Venv 및 Anaconda를 사용하여 다양한 Python 버전을 관리 할 수 있습니다. 1) PYENV를 사용하여 여러 Python 버전을 관리합니다. Pyenv를 설치하고 글로벌 및 로컬 버전을 설정하십시오. 2) VENV를 사용하여 프로젝트 종속성을 분리하기 위해 가상 환경을 만듭니다. 3) Anaconda를 사용하여 데이터 과학 프로젝트에서 Python 버전을 관리하십시오. 4) 시스템 수준의 작업을 위해 시스템 파이썬을 유지하십시오. 이러한 도구와 전략을 통해 다양한 버전의 Python을 효과적으로 관리하여 프로젝트의 원활한 실행을 보장 할 수 있습니다.

Numpyarrayshaveseveraladvantagesstandardpythonarrays : 1) thearemuchfasterduetoc 기반 간증, 2) thearemorememory-refficient, 특히 withlargedatasets 및 3) wepferoptizedformationsformationstaticaloperations, 만들기, 만들기

어레이의 균질성이 성능에 미치는 영향은 이중입니다. 1) 균질성은 컴파일러가 메모리 액세스를 최적화하고 성능을 향상시킬 수 있습니다. 2) 그러나 유형 다양성을 제한하여 비 효율성으로 이어질 수 있습니다. 요컨대, 올바른 데이터 구조를 선택하는 것이 중요합니다.

tocraftexecutablepythonscripts, 다음과 같은 비스트 프랙티스를 따르십시오 : 1) 1) addashebangline (#!/usr/bin/envpython3) tomakethescriptexecutable.2) setpermissionswithchmod xyour_script.py.3) organtionewithlarstringanduseifname == "__"

numpyarraysarebetterfornumericaloperations 및 multi-dimensionaldata, mumemer-efficientArrays

numpyarraysarebetterforheavynumericalcomputing, whilearraymoduleisiMoresuily-sportainedprojectswithsimpledatatypes.1) numpyarraysofferversatively 및 formanceforgedatasets 및 complexoperations.2) Thearraymoduleisweighit 및 ep

ctypesallowscreatingandmanipulatingC-stylearraysinPython.1)UsectypestointerfacewithClibrariesforperformance.2)CreateC-stylearraysfornumericalcomputations.3)PassarraystoCfunctionsforefficientoperations.However,becautiousofmemorymanagement,performanceo


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

Eclipse용 SAP NetWeaver 서버 어댑터
Eclipse를 SAP NetWeaver 애플리케이션 서버와 통합합니다.

WebStorm Mac 버전
유용한 JavaScript 개발 도구

SublimeText3 Linux 새 버전
SublimeText3 Linux 최신 버전

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.
