Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #71424 > unrolled thread
| Started by | Mark H Harris <harrismh777@gmail.com> |
|---|---|
| First post | 2014-05-12 23:41 -0500 |
| Last post | 2014-05-13 11:09 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
Simple Function Decorator Sample Snippet Mark H Harris <harrismh777@gmail.com> - 2014-05-12 23:41 -0500
Re: Simple Function Decorator Sample Snippet Steven D'Aprano <steve@pearwood.info> - 2014-05-13 05:54 +0000
Re: Simple Function Decorator Sample Snippet Mark H Harris <harrismh777@gmail.com> - 2014-05-13 01:04 -0500
Re: Simple Function Decorator Sample Snippet alister <alister.nospam.ware@ntlworld.com> - 2014-05-13 11:09 +0000
| From | Mark H Harris <harrismh777@gmail.com> |
|---|---|
| Date | 2014-05-12 23:41 -0500 |
| Subject | Simple Function Decorator Sample Snippet |
| Message-ID | <lks7pd$gd0$1@speranza.aioe.org> |
hi folks, I've come up with a simple snippet that intends to explain the
concept of decorations without an article (for on app help), while being
succinct and concise, while not being overly complicated.
Does this work? I have another one coming for args, similar, if this
works... comments appreciated. thanks.
> # BEGIN FUNCTION DECORATOR SIMPLE ################################
> #
> # define the function decorator (wrapper function)
> def enter_exit(f):
> def new_f():
> print("entering", f.__name__)
> f()
> print(f.__name__, "exited !", end="\n\n")
> return new_f
>
> # the above "function decoration" takes a 'callable' as an argument
> # and returns a 'callable' new function that is used to
> # replace the original function (function is decorated), which
> # adds functionality to the original function being decorated ...
>
> # define the original function
> def f1():
> print(" inside f1()")
>
> # replace the original function (above) with the new decorated
> # 'wrapped' function using the function decoration 'enter_exit'...
> f1 = enter_exit(f1)
>
> # (OR) accomplish the same thing with decoration lines as below:
>
> # functions wrapped with decoration lines syntax (annotations)
> # as below, accomplish the same 'decoration' as above
> # by using some 'syntactic sugar' to accomplish same ...
>
> @enter_exit
> def f2():
> print(" inside f2()")
>
> @enter_exit
> def f3():
> print(" inside f3()")
>
> # demo the new 'decorated' functions
> f1()
> f2()
> f3()
>
> # END FUNCTION DECORATOR SIMPLE ##################################
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2014-05-13 05:54 +0000 |
| Message-ID | <5371b3a7$0$11109$c3e8da3@news.astraweb.com> |
| In reply to | #71424 |
On Mon, 12 May 2014 23:41:18 -0500, Mark H Harris wrote: > hi folks, I've come up with a simple snippet I don't think that this idea is original to you :-) I'm pretty sure many people have come up with the idea of a decorator that just announces when it runs and when it is called. I know I have :-) People keep independently inventing this because it's an obvious, and easy to understand, example. Nicely done. > that intends to explain the > concept of decorations without an article (for on app help), while being > succinct and concise, while not being overly complicated. > > Does this work? I have another one coming for args, similar, if this > works... comments appreciated. thanks. I tried to run your code, but every line is quoted with a > which causes a syntax error. Was there a reason you quoted the snippet? -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Mark H Harris <harrismh777@gmail.com> |
|---|---|
| Date | 2014-05-13 01:04 -0500 |
| Message-ID | <lksclm$qqi$1@speranza.aioe.org> |
| In reply to | #71431 |
On 5/13/14 12:54 AM, Steven D'Aprano wrote:
> I don't think that this idea is original to you :-) I'm pretty sure many
> people have come up with the idea of a decorator that just announces when
> it runs and when it is called. I know I have :-)
oh, absolutely... every piece of that thing comes from somebody
(about six somebodies to be exact) what makes it unique is that its
compressed, in the right order (I think), and 'without' extraneous
gibberish that confuses the whole dang thing.
>
> People keep independently inventing this because it's an obvious, and
> easy to understand, example. Nicely done.
Its obvious until you get to the @charater. In fact its taken so
obviously by *everyone* that no one actually gets it explained without
eight pages or twelve steps of something until its all confused.
> I tried to run your code, but every line is quoted with a > which causes
> a syntax error. Was there a reason you quoted the snippet?
Yes, because depending on your interface the code can get mangled (the
indentation thing breaks). the quoted paste seems to avoid this mostly
with the downside that the quote characters need to be striped from the
py file. <sorry> by the way, any suggestions are welcome regarding
that too... there doesn't really seem to be a good way to share code on
the group consistently.
marcus
[toc] | [prev] | [next] | [standalone]
| From | alister <alister.nospam.ware@ntlworld.com> |
|---|---|
| Date | 2014-05-13 11:09 +0000 |
| Message-ID | <Q3ncv.34411$GL7.16117@fx10.am4> |
| In reply to | #71435 |
On Tue, 13 May 2014 01:04:39 -0500, Mark H Harris wrote: > Yes, because depending on your interface the code can get mangled (the > indentation thing breaks). the quoted paste seems to avoid this mostly > with the downside that the quote characters need to be striped from the > py file. <sorry> by the way, any suggestions are welcome regarding > that too... there doesn't really seem to be a good way to share code on > the group consistently. use a news reader rather than Google groups BTW documenting decorators is a good idea even if only for your own use. it has taken me a long type to groc decorators & see any reason why I would use one rather than just modifying my function. (I finally got it when mucking about with wsgi where I used a couple of decorators to prepend & append header & footer files to various other functions, reducing code duplication significantly -- WHO sees a BEACH BUNNY sobbing on a SHAG RUG?!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web