function attributes (from WIKI)
"Mark Hahn" <[email protected]>
| Newsgroups | gmane.comp.lang.prothon.user |
|---|---|
| Message-ID | <[email protected]> |
Serge wrote:
Executable part of function seems to be abused for declarations:
def func():
"""func is meant to be used as a command."""
func.command_ = True
blah blah blah
It is not clear how many times assignment to func.command_ will be executed.
I propose the following syntax with obvious meaning:
def func():
attrs:
command_ = True
"""func is meant to be used as a command."""
blah blah blah
Mark:
Interesting concept. No one has ever complained about the docstring code
being executed every call before, although it is less than an assignment.
Doesn't this solve the decorator argument?
No one has ever complained about the docstring code being executed every
call before
probably because it's immutable and therefore can be stored when function is
created.
My location right after the doc string is near the top like you want. You
have not shown me why it will not work there.
It works but there are problems with mutable attributes, does the following
code mean the attribute will be reset every time the function is called?
def func(key,value):
func.attr = {}
and this :
def Button(parent).callback():
???.attr = {}
Doesn't this solve the decorator argument?
I'd like to forget about the word 'decorator'. Let's talk about attribute
setting and wrappers. Attribute setting already exists in Prothon, I'm just
pointing out the problems current approach has. Wrappers are different
beasts. See my comment at DecoratorFeature
Mark:
I think I remember losing an argument to Paul about using attributes as
decorators. My mind is as bad at archiving discussions as the mailing lists
are. Not only does the attribute assignment run each time the function runs,
but the worse problem is that it doesn't run before the first run of the
function! So you are right that putting a simple attribute assignment inside
a function is not a wrapper or decorator replacement.
Maybe your new idea about a keyword inside a function could "fix" my bad
idea. Maybe general code could go there that could execute at definition
time but be placed inside the function at the top. Maybe the doc string
should be part of this new feature.
There is another idea that I have been thinking of that has to do with the
beginning of functions. I don't know if it could be combined with the other
idea. It has to do with lazy initialization of things inside of functions.
It would be nice if we could have an optimization for lazy initialization
built into the language:
# old method
def func():
if not func.attrs_.hasKey($staticVar):
func.staticVar = 0
func.staticVar +! 1
#new proposal
def func():
first:
func.staticVar = 0
func.staticVar +! 1
The problem with the old code is that the expensive attribute check would
run on every function call when it is only needed the very first call.
Code inside the "first" block would only run the very first time the
function was ever called. Any code could be put into that block and it would
only run once.
These two ideas have much in common. They both run code exactly once. One
runs code at definition time and the second runs code at the first calling
time. Maybe one keyword could be called defInit and one runInit. I'll start
a new feature request for 1.0 with these ideas of yours and mine.