Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #7095
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'pypi': 0.04; 'slices': 0.07; 'terry': 0.07; 'python': 0.08; '*and*': 0.09; 'assert': 0.09; 'function:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'recursion': 0.09; 'successive': 0.09; 'tail': 0.09; 'valueerror:': 0.09; 'output': 0.11; 'def': 0.12; 'written': 0.14; 'am,': 0.14; 'wrote:': 0.14; "'n'": 0.16; 'arg': 0.16; 'docstring': 0.16; 'input,': 0.16; 'into.': 0.16; 'lambda': 0.16; 'n):': 0.16; 'reedy': 0.16; 'succinct': 0.16; 'argument': 0.16; 'yield': 0.19; 'jan': 0.20; 'header:In-Reply- To:1': 0.21; 'code.': 0.22; 'subject:question': 0.23; 'correct,': 0.23; 'null': 0.23; 'welcome.': 0.23; 'code': 0.24; 'function': 0.25; 'statement': 0.26; 'tests': 0.26; 'skip:[ 10': 0.26; 'definition': 0.26; 'thanks': 0.28; 'raise': 0.28; 'problem': 0.28; 'expressions': 0.29; 'good.': 0.29; 'cases.': 0.30; 'i/o': 0.30; 'ago': 0.31; 'this.': 0.31; 'clearly': 0.32; 'cases': 0.32; 'expression': 0.32; 'header:X-Complaints-To:1': 0.32; 'does': 0.33; 'to:addr:python-list': 0.33; 'post': 0.33; 'error': 0.33; 'thank': 0.35; 'example,': 0.35; 'header:User-Agent:1': 0.35; 'fails': 0.35; 'sense,': 0.35; 'else': 0.35; 'test': 0.35; 'something': 0.37; 'change': 0.37; 'designing': 0.37; 'ways': 0.37; 'think': 0.38; 'received:org': 0.38; 'could': 0.38; 'positive': 0.38; 'third': 0.38; 'subject:: ': 0.38; 'doing': 0.39; 'should': 0.39; 'said': 0.39; 'it!': 0.39; 'header:Mime- Version:1': 0.39; 'got': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'everyone': 0.40; 'really': 0.40; 'help': 0.40; 'more': 0.60; 'your': 0.60; 'year': 0.61; 'back': 0.63; 'taking': 0.64; 'incredible': 0.65; 'plus': 0.65; 'here': 0.66; 'share': 0.67; 'accepts': 0.68; 'safe': 0.69; 'packing': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: Lambda question |
| Date | Mon, 06 Jun 2011 12:52:31 -0400 |
| References | <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | quoted-printable |
| X-Gmane-NNTP-Posting-Host | rain.gmane.org |
| User-Agent | Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 |
| In-Reply-To | <20110606134215.3EYA9.180297.root@cdptpa-web25-z02> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2494.1307379170.9059.python-list@python.org> (permalink) |
| Lines | 79 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1307379171 news.xs4all.nl 49046 [::ffff:82.94.164.166]:43590 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:7095 |
Show key headers only | View raw
On 6/6/2011 9:42 AM, jyoung79@kc.rr.com wrote:
>>>>>> f = lambda x, n, acc=[]: f(x[n:], n, acc+[(x[:n])]) if x else acc
>
>> Packing tail recursion into one line is bad for both understanding and
>> refactoring. Use better names and a docstring gives
>>
>> def group(seq, n):
>> 'Yield from seq successive disjoint slices of length n plus the
>> remainder'
>> for i in range(0,len(seq), n):
>> yield seq[i:i+n]
[I added back the last 'n' that got deleted somehow]
> Thank you all very much for this incredible help! The original code
> now makes sense, and I was thrilled to see better and more efficient
> ways of doing this. Thanks for taking the time to share your
> thoughts as well as the excellent detail everyone shared… I really
> appreciate it!
You are welcome.
Let me add something not said much here about designing functions: start
with both a clear and succinct definition *and* test cases. (I only
started writing tests first a year ago or so.) Test cases help test the
definition statement as well as the yet-to-be-written code. They also
make re-factoring much safer. I think test cases should start with null
inputs. For this function:
for inn,out in (
(('',1), []), # no input, no output
(('abc',0), []), # definition unclear, could be error
(('abc',1), ['a','b','c']),
(('abcd',2), ['ab','cd']),
(('abcde',2), ['ab', 'cd', 'e']), # could change this
):
assert list(group(*inn)) == out, (inn,out)
This fails with
ValueError: range() arg 3 must not be zero
I will let you think about and try out what the original code 'f=../
does with n=0. It is not good. A third problem with lambda expressions
is no test for bad inputs. They were added to Python for situations
where one needs a function as an argument and and the return expression
is self-explanatory, clearly correct, and safe for any inputs it could
get in the context it is passed into. For example, lambda x: 2*x.
This works:
def group(seq, n):
'Yield from seq successive disjoint slices of length n & the remainder'
if n<=0: raise ValueError('group size must be positive')
for i in range(0,len(seq), n):
yield seq[i:i+n]
for inn,out in (
(('',1), []), # no input, no output
#(('abc',0), ValueError), # group size positive
(('abc',1), ['a','b','c']),
(('abcd',2), ['ab','cd']),
(('abcde',2), ['ab', 'cd', 'e']), # could change this
):
assert list(group(*inn)) == out, (inn,out)
I have written a function test function that I will post or upload to
PyPI sometime. It accepts i/o pairs with error 'outputs', like the one
commented out above.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Lambda question Terry Reedy <tjreedy@udel.edu> - 2011-06-06 12:52 -0400
Re: Lambda question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-06 23:16 +0000
API design before implementation (was: Lambda question) Ben Finney <ben+python@benfinney.id.au> - 2011-06-07 09:54 +1000
Re: Lambda question harrismh777 <harrismh777@charter.net> - 2011-06-06 19:00 -0500
csiph-web