메타클래스를 작성할 때 특별 메서드 인수에 다음과 같은 명명 관례를 채택하면 도움이 된다고 생각한다.
●
인스턴스 메서드에는
self
대신
cls
를 사용한다. 인스턴스가 클래스이기 때문이다.
●
클래스 메서드에
cls
대신
meta
_
cls
를 사용한다. 클래스가 메타클래스이기 때문이다.
__
new
__
()
메서드는
@
classmethod
데커레이터가 없지만, 클래스 메서드로서 작동함에 주의하라.
예제
24-18
metalib
.
py
:
MetaKlass
클래스
class MetaKlass(type):
print(‘% MetaKlass body’)
@classmethod ❶
def __prepare__(meta_cls, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.