-
__slot__, __call__python/응용 2023. 4. 10. 23:42
__slot__
python 에서는 runtime 에 class attribute 를 추가할 수 있다. 그런데 아무거나 마구 추가할 수 없도록 __slots__ 으로 제한할 수 있다. __slot__ 에 정의된 이름의 attribute 만 정의(or 추가) 될 수 있다. (__slot__ 은 class attibute 목록 명세?)
class Dog: __slots__ = ['name', 'age'] dog = Dog() dog.name = 'apple' # ok dog.age = 1 # ok dog.gender = 'm' # ERROR 'gender' is not in __slots__
__call__
scala 의 apply() method 랑 비슷?
class Dog: def __call__(self, x, y): print(x * 10 + y) dog = Dog() dog(3, 5) # 35
'python > 응용' 카테고리의 다른 글
jinja2 template 으로 Html 본문작성 후 이메일 보내기 (0) 2023.05.27 자주하는 실수 (0) 2023.05.07 yield from (0) 2023.04.10 ABC vs Protocol (0) 2023.04.01 ThreadPoolExecutor (0) 2023.02.09