Why ELIF?

TerryP bigboss1964 at gmail.com
Mon Oct 12 00:52:19 EDT 2009


On Oct 11, 9:43 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Sun, 11 Oct 2009 11:15:06 -0700, TerryP wrote:
> > I might take flak here, for writing something like 'dict[key]
> > (func_args)' instead of something more Pythonic,
>
> Looking up a first-class function in a dictionary and passing arguments
> to it is perfectly Pythonic.
>

It's a technique (learned through Perl), that greatly changed the way
I think about writing code. Since then I've looked for ways to tell
programming languages how to work harder, so that we can sit on our
bums more often.


My brain thinks about programming problems more abstractly then any
specific language, so I pay more attention to the art of it, then to
the idiomatic usage of language foo. As such, that also means I look
for good code instead of code that obeys some pattern or mantra - and
have never claimed to know what "Pythonic" code actually looks
like ;).



On Oct 12, 1:34 am, John Machin <sjmac... at lexicon.net> wrote:
> MRAB <python <at> mrabarnett.plus.com> writes:
>
>
>
> > Simon Forman wrote:
> > [snip]
>
> > > I'll often do that this way:
>
> > > args = re.split('\s', line)
>
> > This has the same result, but is shorter and quicker:
>
> > args = line.split()
>
> HUH?
>
> Shorter and quicker, yes, but provides much better functionality; it's NOT the
> same result:
>
>  >>> line = '   aaa   bbb   ccc   '
>  >>> import re
>  >>> re.split('\s', line)
>  ['', '', '', 'aaa', '', '', 'bbb', '', '', 'ccc', '', '', '']
>  >>> line.split()
>  ['aaa', 'bbb', 'ccc']
>  >>>


As I expresssed, the code served make a point not teach. Brevity is
sometimes done for it's own sake. In real code I would do something
more like:

  cmd, args = some_self_documenting_and_fully_qualified_function_name
(line)

and that would handle tokenizing the line correctly throughout the
body of code, for invocation anywhere that it is needed. I hate to
repeat myself when I can compose functions instead.



I chose the re.split() on '\s' whitespace bit as a short cut in
expressing what the most common case would have looked like without it
having to detract from the OP's topic. It also (subtly) implies that
one must look beyond the lotus flower in order to obtain enlightenment
\o/.


P.S. Mick Krippendorf, thanks I forgot about 'import this' :-}

--
TerryP.



More information about the Python-list mailing list