What is it called and how can this be done (`function_name.decorator`)?

It is a pity the extreme stupid name, but if I know what it is, I would not write here (:

def some_decorator( func ):
    # ..

class A:
    @some_decorator
    def func():
        pass
    @func.some_decorator    # this one here - func.some_decorator ?
    def func():
        pass

some_decoratordecorates func- it's OK. But what is it func.some_decoratorand how does it some_decoratorbecome a member (or something else?) func?

PS I'm 90% sure that there is such a question (as it seems to be something basic), but I do not know how to look for it. If there is an exact duplicate, I will delete this question.


Note. It is not a typo or an accident that both member functions are called func. The decorator is designed for overloading: the question is related to the decoration method (overloading class methods)

+3
source share
2 answers

, :

def func():
    pass
func = some_decorator(func)

, func , , , . , , . , some_decorator ( , some_decorator func - , ), . @, , func. . :

class A:
    def func():
        pass
    func = some_decorator(func)

    _decorator = func.some_decorator
    def func():
        pass
    func = _decorator(func)
+7

- , : property:

class C(object):
    @property
    def x(self):
        "This is a property object, not a function"
        return self._x
    @x.setter
    def x(self, val):
        self._x = val

>>> c = C()
>>> c.x = 1
>>> c.x
1
>>> C.x
<property object at 0x2396100>
>>> C.x.__doc__
'This is a property object, not a function'
>>> C.x.getter.__doc__
'Descriptor to change the getter on a property.'
>>> C.x.setter.__doc__ 
'Descriptor to change the setter on a property.'
>>> C.x.deleter.__doc__
'Descriptor to change the deleter on a property.'

property ( ) , x - . , fget, fset fdel , property.setter property.deleter (, , ).

- , . , , , (, , -, property ).

+2

All Articles