[Edu-sig] guide to learning decorators

kirby urner kirby.urner at gmail.com
Mon Jul 9 07:50:27 CEST 2012


On Sun, Jul 8, 2012 at 10:03 PM, Matt Harrison
<matthewharrison at 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


More information about the Edu-sig mailing list