[Tutor] Tutor Digest, Vol 161, Issue 36

Borisco Bizaro boriscobizaro8 at gmail.com
Mon Jul 24 13:41:41 EDT 2017


Please what is the best way to study python programming well.
On Jul 24, 2017 17:00, <tutor-request at python.org> wrote:

> Send Tutor mailing list submissions to
>         tutor at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         https://mail.python.org/mailman/listinfo/tutor
> or, via email, send a message with subject or body 'help' to
>         tutor-request at python.org
>
> You can reach the person managing the list at
>         tutor-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Tutor digest..."
>
> Today's Topics:
>
>    1. Re: Python3 Help (Alan Gauld)
>    2. basic decorator question (bruce)
>    3. Re: basic decorator question (Mats Wichmann)
>
>
> ---------- Forwarded message ----------
> From: Alan Gauld <alan.gauld at yahoo.co.uk>
> To: tutor at python.org
> Cc:
> Bcc:
> Date: Mon, 24 Jul 2017 09:27:18 +0100
> Subject: Re: [Tutor] Python3 Help
> On 24/07/17 01:58, Alan Gauld via Tutor wrote:
>
> > $ which python3
> >
> >>      -bash: $: command not found
> >
> > The $ is the OS prompt you are not supposed to type it in.
>
> While on the subject you might also see something like
>
> # <some command>
>
> Which can mean one of two things
> 1) It's a comment and you should not type it in
> 2) It's a root level command and you should su to root
>    before running it. (# was the default Unix prompt
>    for super users)
>
> The latter usage is dying out and usually replaced with
>
> $ sudo <some command>
>
> Which means that as an ordinary user ($) you type sudo
> before the command. sudo should then prompt for your
> user password before carrying out the command.
>
> But the older # prompt style is still around in some
> onlne tutorials.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
>
>
>
> ---------- Forwarded message ----------
> From: bruce <badouglas at gmail.com>
> To: Python Tutor Mailing List <tutor at python.org>
> Cc:
> Bcc:
> Date: Mon, 24 Jul 2017 10:33:25 -0400
> Subject: [Tutor] basic decorator question
> Hi.
>
> I've seen sites discuss decorators, as functions that "wrap" and
> return functions.
>
> But, I'm sooo confuzed! My real question though, can a decorator have
> multiple internal functions? All the examples I've seen so far have a
> single internal function.
>
> And, if a decorator can have multiple internal functions, how would
> the calling sequence work?
>
> But as a start, if you have pointers to any really "basic" step by
> step sites/examples I can look at, I'd appreciate it. I suspect I'm
> getting flumoxed by something simple.
>
> thanks
>
>
>
> ---------- Forwarded message ----------
> From: Mats Wichmann <mats at wichmann.us>
> To: bruce <badouglas at gmail.com>, Python Tutor Mailing List <
> tutor at python.org>
> Cc:
> Bcc:
> Date: Mon, 24 Jul 2017 09:49:03 -0600
> Subject: Re: [Tutor] basic decorator question
> On 07/24/2017 08:33 AM, bruce wrote:
> > Hi.
> >
> > I've seen sites discuss decorators, as functions that "wrap" and
> > return functions.
> >
> > But, I'm sooo confuzed! My real question though, can a decorator have
> > multiple internal functions? All the examples I've seen so far have a
> > single internal function.
> >
> > And, if a decorator can have multiple internal functions, how would
> > the calling sequence work?
> >
> > But as a start, if you have pointers to any really "basic" step by
> > step sites/examples I can look at, I'd appreciate it. I suspect I'm
> > getting flumoxed by something simple.
>
> wrap and return are not two distinct things, they're part of the same
> process...  the general concept is that a decorator changes the result
> of a function without modifying the function itself by returning a new
> function object which does some other stuff in addition to running the
> code of the original function object.
>
> This is a really simple wrapper:
>
> def my_decorator(some_function):
>     def wrapper():
>         print("Stuff happening before some_function() is called.")
>         some_function()
>         print("Stuff after some_function() is called.")
>     return wrapper
>
> If you have an unwrapped function:
>
> def foo():
>     print "This is the unwrapped function"
>
> You can show this in action like this:
>
> foo()
> bar = my_decorator(foo)
> bar()
>
> function names are just handles to the function object, so the middle
> line of those three is passing the original function object referred to
> by foo to my_decorator, whose inner function returns a function object
> which is runs some code before and after the original function.  If the
> undecorated fuction does not need to be referred to, the previous often
> gets written as:
>
> foo = my_decorator(foo)
> foo()
>
> Now to add Python's magical decorator syntax:
>
> @my_decorator
> def bar():
>     print "This is another unwrapped function"
>
> bar()
>
> So all the @my_decorator bit does is provide shorthand for the syntax
>
> bar = my_decorator(bar)
>
> Wasn't ultra-clear on your original question; if you wanted the
> "happening before" and "happening after" to call out to other functions
> instead of doing a print, you can. Is that what you mean by multiple
> internal functions?
>
> Does this clarify at all?
>
> Do hunt some, there are some really good tutorials on decorators.
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> https://mail.python.org/mailman/listinfo/tutor
>
>


More information about the Tutor mailing list