April 19, 2004
Python recipes

There have been a couple of very interesting Python recipes added to the Python Cookbook recently.

I love Michele Simionato's Parsing the command line. Parsing the module's docstring for the command line options is a brilliant idea. It feels really pythonic; most other languages, for example, treat indentation as purely documentary, and require you to repeat your code block delimitation using delimitation characters and using indentation, whereas Python treats the indentation as definitive, and doesn't require you to repeat yourself - DRY! Similarly, this recipe treats the docstring's definition of the supported command line options as definitive, and doesn't require you to repeat yourself.

Raymond Hettinger's Bind Globals into Constants at compile time is a lovely example of the kind of thing that Python 2.4's method decorators are designed to support.

I have to say, I'm not that keen on the current decorator syntax; a list of decorators on the line preceding the function/method definition looks ugly to me. I'd prefer the decorators in the def statement somewhere, or inside the function somewhere, like the docstring. But what do I know; Guido's the world-class language designer, after all!

Posted to Python by Simon Brunning at April 19, 2004 12:43 PM
Comments

Regarding the def syntax, there's a PEP that covers what you're looking for:

http://mail.python.org/pipermail/python-dev/2004-February/042851.html

Aaron

Posted by: Aaron Brady on April 19, 2004 04:47 PM

I know. But if Guido likes the "list of decorators on the line preceding the function/method definition" syntax, that's what we'll get.

I'm not worried. It looks wrong to me at the moment, but I find that I almost always end up coming around to Guido's way of seeing things in the end. He has an irritating habit of being right all the time. Well, *almost* all the time, anyway.

Posted by: Simon Brunning on April 19, 2004 04:58 PM

I found "a list on the line before" to be very disconcerting at first. But the more I play with it, the better I like it. And the fact that it looks quite similar to what C# has is a convenient coincidence.

Posted by: Michael Chermside on April 19, 2004 05:28 PM

well, delimiters does not need to be useless.
If the language is expression oriented you may see code added in the end just logically, say, with statement modifiers:

def f
...
end if NEED_F_REDEFINED

Posted by: gabriele on April 19, 2004 09:51 PM

Sorry, Gabriele, but I have *no* idea what that's all about. Conditioned ends? The mind boggles!

Posted by: Simon Brunning on April 20, 2004 10:45 AM

WRT Michele Simionato's command-line Parsing recipe, I've seen the same trick used in shell scripts. You just put a nice header comment at the start, ending with "##", and add two lines that look somewhat like this:

me=$0
function usage { sed -n '2,/^##$/s/^# //p' $me ; exit 1; }

(This works in zsh, haven't tried other shells, but most of them could do similar stuff.)

Posted by: Doug L. on April 20, 2004 08:23 PM

simon: if you enter the 'everything is an expression' mood you'll understand that that code mean
'define method "f" if you need it'.

Actually, I have to agree, that was not a so good example.
And Actually I can't think of any ATM :)
Apart from allowing just the existence of eval() over eval()+exec() in languages that does not separate statements and expressions.

maybe this:
[1,2,3].map do |i|
i**2
end.reject do |i|
i%2 == 2
end


(yes I know that a list comprehension does this cleaner it's just an example of using delimiters to concatenate syntax)

I really believe a mixed significant-space/optional delimeters syntax would be the best thing, haskell does like this IIRC

Posted by: gabriele on April 21, 2004 04:17 PM

That's easy enough, Gabriele:

if some_condition:
    def some_function(argument):
        pass

Significant spaces *and* delimiters? Thanks, but no thanks. TOOWTDI.

Posted by: Simon Brunning on April 21, 2004 04:28 PM

lincoln ls assessories ls spoilers hoodcanalbridge shiawassee county michigan pics of almost nude ladies

Posted by: Future_dim on October 29, 2008 06:38 AM

Posted by: Future_dim on November 1, 2008 04:22 AM

Posted by: Future_dim on November 4, 2008 02:30 PM

lewes cape may ferry mercruiser chevy 6 cylinder deleted history eab male dogs in heat

Posted by: Mary-xp on November 6, 2008 06:19 PM

texas unemployment compersation act hostile woking emviroment hegner saw decorating baseball dugouts joyce stenn

Posted by: Mary-ss on November 7, 2008 05:33 AM

texas unemployment compersation act hostile woking emviroment hegner saw decorating baseball dugouts joyce stenn

Posted by: Mary-ss on November 7, 2008 05:33 AM

ny new play reading work schedule templates pound cake mix recipe conversion

Posted by: Mary-hz on November 12, 2008 01:33 PM

tyler seiswerda alex bell fort lauderdale gun shop

Posted by: Mary-bh on November 12, 2008 06:40 PM

tyler seiswerda alex bell fort lauderdale gun shop

Posted by: Mary-bh on November 12, 2008 06:40 PM

tyler seiswerda alex bell fort lauderdale gun shop

Posted by: Mary-bh on November 12, 2008 06:40 PM

funeral for soldiers killed in iraq mothers day cards for friends bill gates photos

Posted by: Mary-yf on November 13, 2008 08:19 AM

funeral for soldiers killed in iraq mothers day cards for friends bill gates photos

Posted by: Mary-yf on November 13, 2008 08:19 AM
Post a comment
Name:


Email Address:


URL:



Comments:


Remember info?