Re: [Edu-sig] guide to learning decorators
Re: http://www.amazon.com/Guide-Learning-Python-Decorators-ebook/dp/B006ZHJSIM I got the Kindle book on Python decorators. Quite inexpensive. I didn't give it my highest rating though, for two reasons: (a) there's some representing that an exhaustive treatment of the function arguments and parameters topic, but the use of * (asterisk) on its own, to section off keyword-only parameters, is not mentioned and (b) the authors hint at the many useful and wonderful things decorators might do, but are somewhat shy on examples. One would think just a few more pages of clever examples would round out the treatment. Other than those two omissions, I thought it was pretty strong. The main goal was to get to where your decorator function might itself take arguments, what that means. There are no examples of using a class as a decorator that I recall, nor of decorating a class. I should double check that. Kirby
Hey Kirby- Author of said book replying :) On Sun, Jul 8, 2012 at 10:39 PM, kirby urner <kirby.urner@gmail.com> wrote:
Re: http://www.amazon.com/Guide-Learning-Python-Decorators-ebook/dp/B006ZHJSIM
I got the Kindle book on Python decorators.
Thanks!
Quite inexpensive.
Yep, that might change as I'm in the middle of revamping my books.
I didn't give it my highest rating though, for two reasons:
(a) there's some representing that an exhaustive treatment of the function arguments and parameters topic, but the use of * (asterisk) on its own, to section off keyword-only parameters, is not mentioned
I'm not sure I'm parsing this correctly. Could you elaborate?
and
(b) the authors hint at the many useful and wonderful things decorators might do, but are somewhat shy on examples. One would think just a few more pages of clever examples would round out the treatment.
Yep, this is a common complaint, and one that I'm addressing in the revamp.
Other than those two omissions, I thought it was pretty strong. The main goal was to get to where your decorator function might itself take arguments, what that means.
There are no examples of using a class as a decorator that I recall, nor of decorating a class. I should double check that.
You are correct. That is an omission that is somewhat on purpose. To have properly behaving decorators using classes requires understanding of descriptors. So it would complicate the book a bit. As people already cower when they hear decorators, I wanted to have a method that was approachable and worked reliably. Using classes as decorators is more grokable in some's opinion. But not reliable. I'm of the opinion that closures are useful and understandable on their own and that once they are understood, decorators fall out quite easily. And if closures make sense, then parameterized decorators are pretty easy as well. As decorator blog posts pop up quite often I like to keep abreast of this stuff. It seems to be a topic that interests many Python developers. I've used this method of teaching closures first to great success at my PyCon tutorials and have gotten good feedback. Part of my revamping is to include my iterator/generator book, decorator book, and a forthcoming book on functional programming constructs and list comprehensions/generator expressions in a larger tome. I'll probably include descriptors and classes as decorators in that. I'd love feedback from anyone on this list, and would get them a review/beta copy in return. thanks again for the review! -matt Kirby
_______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig
On Sun, Jul 8, 2012 at 10:03 PM, Matt Harrison <matthewharrison@gmail.com> wrote:
Hey Kirby-
Author of said book replying :)
Wow fast. I wondered if you'd spider over here for a chat (smile), sensing vibrations in the Web.
I didn't give it my highest rating though, for two reasons:
(a) there's some representing that an exhaustive treatment of the function arguments and parameters topic, but the use of * (asterisk) on its own, to section off keyword-only parameters, is not mentioned
I'm not sure I'm parsing this correctly. Could you elaborate?
There's this new syntax that allows you to specify that certain parameters may only be set with keyword syntax (not positionally), yet they don't have default values. I don't think your book covered that wrinkle.
def hodgepodge(a, b, *, c, d, e=1, f='f', **g): # note asterisk alone as a parameter print(a, b, c, d, e, f, g, sep="\n")
hodgepodge(1, 2, 3, 4) # try to reach c, d positionally Traceback (most recent call last): File "<pyshell#26>", line 1, in <module> hodgepodge(1, 2, 3, 4) TypeError: hodgepodge() takes exactly 2 positional arguments (4 given)
hodgepodge(1, 2, c=3, d=4) # fill them in, the rest have defaults or aren't mandatory (**g is happy to stay empty) 1 2 3 4 1 f {}
hodgepodge(1, 2, c=3, d=4, e=5, cat="dog") 1 2 3 4 5 f {'cat': 'dog'}
hodgepodge(1, 2) # c and d can't be left alone though, unlike typical keyword parameters, which have defaults Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> hodgepodge(1, 2) TypeError: hodgepodge() needs keyword-only argument c
hodgepodge(1, 2, c=3) Traceback (most recent call last): File "<pyshell#30>", line 1, in <module> hodgepodge(1, 2, c=3) TypeError: hodgepodge() needs keyword-only argument d
hodgepodge(1, 2, c=3, d=4) # gotta mention 'em both
Thanks for sharing more about your plans. I am excited to get up to speed on a Kindle (recent acquisition) in a way that boosts my Python and Bucky worlds, also Portland. In chronological order, my downloads have been: The Lost Inventions of Buckminster Fuller and Other Essays Blake, Trevor June 30, 2012 Portland Memorials Blake, Trevor June 30, 2012 Guide to: Learning Python Decorators Harrison, Matt June 30, 2012 The Time Machine Wells, H. G. (Herbert George) July 1, 2012 The Complete Works of Edgar Allan Poe (Includes Essay About the History of the Horror Genre) Poe, Edgar Allan, Golgotha Press July 1, 2012 Kirby
participants (2)
-
kirby urner
-
Matt Harrison