[Python-Dev] More concerns about decorators
Roman Suzi
rnd at onego.ru
Mon Aug 16 17:39:54 CEST 2004
On Mon, 16 Aug 2004, Michael Hudson wrote:
>Bob Ippolito <bob at redivi.com> writes:
>
>>> Is it possible to change function inplace? Is it even an option?
>>
>> Not sure what you mean. Functions have a __doc__ slot and a __dict__
>> slot that are read/write. __name__ is read-only.
>
>__name__ is writable as of a few days ago.
I mean that decorators are... decorators. They by their meaning must not
change the function but add some twists to their behaviour. Many of decorator
examples are destructing function attributes.
Good example of non-destructing decorator is staticmethod:
>>> class A:
... def m(x):
... """I am method m"""
... m.__security__ = "low"
... m = staticmethod(m)
...
>>> A.m.__doc__
'I am method m'
>>> A.m.__security__
'low'
So, my concern was that it requires additional efforts to save
function attributes. My thought was that probably decorators could
work directly on the function, not changing id of the function?
(staticmethod changes id, of course). Why? Because the function
is not accessible any more. while the old way it was possible to store
intermediate result:
class A:
def m(x):
"""I am method m"""
mm = m
m = staticmethod(m)
And use it somehow. But with decorator function changes. Yes, it is possible
that some decorators could store function internally for the purpose of
calling it, but others will not.
The transfer of function attributes is on the developer. And he/she could be
clueless that docstrings, etc, could be lost in transformation.
Sincerely yours, Roman Suzi
--
rnd at onego.ru =\= My AI powered by GNU/Linux RedHat 7.3
More information about the Python-Dev
mailing list