attrs): # If this isn't a subclass of Model, don't do anything special. … https://github.com/django/django/blob/e27211a0deae2f1d402537f0ebb64ad4ccf6a4da/django/core/meta/__init__.py#L394 githubܦ༝Ͱ༰қʹͨͲΕͨi18nϒϥϯνͬͯݺΕͯͨόʔδϣϯʢ2005/12ʣͷͷ ←typeΛܧঝ͍ͯͯ͠ɺ __new__ ϝιου͕ఆٛ͞Ε͍ͯΔ ϝλΫϥε
return self._value @value.setter def value(self, value): if type(value) is not int: raise ValueError(f"value should be int, but {value}") if value < 0: raise ValueError(f"value souldn't be negative") self._value = value >>> spam = Spam() >>> spam.value = 1 >>> spam.__dict__ {'_value': 1} https://github.com/tsuyukimakoto/pyconjp2020/blob/master/notebook/03.property_01.ipynb >>> spam.value = "10" --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-3-e924802b48db> in <module> ----> 1 spam.value = "10" <ipython-input-1-abf3b6d8088e> in value(self, value) 10 def value(self, value): 11 if type(value) is not int: ---> 12 raise ValueError(f"value should be int, but {value}") 13 if value < 0: 14 raise ValueError(f"value souldn't be negative") ValueError: value should be int, but 10
return self._value @value.setter def value(self, value): if type(value) is not int: raise ValueError(f"value should be int, but {value}") if value < 0: raise ValueError(f"value souldn't be negative") self._value = value >>> spam = Spam() >>> spam.value = 1 >>> spam.__dict__ {'_value': 1} https://github.com/tsuyukimakoto/pyconjp2020/blob/master/notebook/03.property_01.ipynb >>> spam.value = -1 --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-4-3b6deb05698c> in <module> ----> 1 spam.value = -1 <ipython-input-1-abf3b6d8088e> in value(self, value) 12 raise ValueError(f"value should be int, but {value}") 13 if value < 0: ---> 14 raise ValueError(f"value souldn't be negative") 15 self._value = value ValueError: value souldn't be negative >>> spam.value = 1 >>> spam.__dict__ {'_value': 1}
registry[cls.__name__] = cls class AutoRegisterMeta(type): def __new__(cls, name, bases, class_dict): newclass = type.__new__(cls, name, bases, class_dict) register(newclass) return newclass class AbstractPlugin(ABC): @abstractmethod def run(self): pass class MyClassA(AbstractPlugin, metaclass=AutoRegisterMeta): def run(self): return f"return from {self.__class__.__name__}.run" --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-19-daaad246990d> in <module> 18 19 ---> 20 class MyClassA(AbstractPlugin, metaclass=AutoRegisterMeta): 21 def run(self): 22 return f"return from {self.__class__.__name__}.run" TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases https://github.com/tsuyukimakoto/pyconjp2020/blob/master/notebook/06_metaclass_01.ipynb