@decorator

  • https://peps.python.org/pep-0318/
@dec2
@dec1
def func(arg1, arg2, ...):
    pass
def func(arg1, arg2, ...):
    pass
func = dec2(dec1(func))

@property

  • https://docs.python.org/3/library/functions.html#property
  • class property(fget=None, fset=None, fdel=None, doc=None)
  • fget: getter: x
  • fset: setter: x=a
  • fdel: deleter: del x
class Property:
    "Emulate PyProperty_Type() in Objects/descrobject.c"

    def __init__(self, fget=None, fset=None, fdel=None, doc=None):
        self.fget = fget
        self.fset = fset
        self.fdel = fdel
        if doc is None and fget is not None:
            doc = fget.__doc__
        self.__doc__ = doc
        self._name = ''

    def __set_name__(self, owner, name):
        self._name = name

    def __get__(self, obj, objtype=None):
        if obj is None:
            return self
        if self.fget is None:
            raise AttributeError(f"property '{self._name}' has no getter")
        return self.fget(obj)

    def __set__(self, obj, value):
        if self.fset is None:
            raise AttributeError(f"property '{self._name}' has no setter")
        self.fset(obj, value)

    def __delete__(self, obj):
        if self.fdel is None:
            raise AttributeError(f"property '{self._name}' has no deleter")
        self.fdel(obj)

    def getter(self, fget):
        prop = type(self)(fget, self.fset, self.fdel, self.__doc__)
        prop._name = self._name
        return prop

    def setter(self, fset):
        prop = type(self)(self.fget, fset, self.fdel, self.__doc__)
        prop._name = self._name
        return prop

    def deleter(self, fdel):
        prop = type(self)(self.fget, self.fset, fdel, self.__doc__)
        prop._name = self._name
        return prop

with

  • https://peps.python.org/pep-0343/
  • __enter__, __exit__ method required
with EXPR as VAR:
    BLOCK
VAR = EXPR
VAR.__enter__()
try:
    BLOCK
finally:
    VAR.__exit__()