On 07-05-18 17:45, Peter Otten wrote:
> Antoon Pardon wrote:
>
>> On 05-05-18 09:33, Peter Otten wrote:
>>> I think you have established that there is no straight-forward way to
>>> write this as a lambda. But is adding a default to itemgetter the right
>>> conclusion?
>>>
>>> If there were an exce
Antoon Pardon wrote:
> On 05-05-18 09:33, Peter Otten wrote:
>> I think you have established that there is no straight-forward way to
>> write this as a lambda. But is adding a default to itemgetter the right
>> conclusion?
>>
>> If there were an exception-catching decorator you could write
>>
>>
On 05-05-18 09:33, Peter Otten wrote:
> I think you have established that there is no straight-forward way to write
> this as a lambda. But is adding a default to itemgetter the right
> conclusion?
>
> If there were an exception-catching decorator you could write
>
> f = catch(IndexError, "spam")
On Fri, May 4, 2018 at 5:34 PM, Thomas Jollans wrote:
> On 04/05/18 22:38, Ian Kelly wrote:
>> The real thing is written in C.
>>
>
> Is it though?
>
> https://github.com/python/cpython/blob/a1fc949b5ab8911a803eee691e6eea55cec43eeb/Lib/operator.py#L265
It is. First, notice the docstring of that m
05.05.18 12:59, Steven D'Aprano пише:
On Sat, 05 May 2018 10:31:17 +0300, Serhiy Storchaka wrote:
Consider a concrete example. You need to sort a list of 2- or 3- element
tuples by the first and third items (third items are strings if
presented). itemgetter(0, 2) doesn't work because some tuples
On Sat, 05 May 2018 10:31:17 +0300, Serhiy Storchaka wrote:
> Consider a concrete example. You need to sort a list of 2- or 3- element
> tuples by the first and third items (third items are strings if
> presented). itemgetter(0, 2) doesn't work because some tuples has only 2
> items. But you can u
On Sat, 05 May 2018 09:33:37 +0200, Peter Otten wrote:
> I think you have established that there is no straight-forward way to
> write this as a lambda.
What I *mostly* established was that I was having a "cannot brain, I have
the dumb" day, because the solution with ternary if was obvious in
h
Steven D'Aprano wrote:
> A re-occurring feature request is to add a default to itemgetter and
> attrgetter. For example, we might say:
>
> from operator import itemgetter
> f = itemgetter(1, 6, default="spam") # proposed feature
> f("Hello World!") # returns ('e', 'W')
> f("Hello") # re
04.05.18 20:04, Steven D'Aprano пише:
On Fri, 04 May 2018 09:17:14 -0600, Ian Kelly wrote:
On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano
wrote:
Here are the specifications:
* you must use lambda, not def;
Why? This seems like an arbitrary constraint.
You'll have to ask the two core devs
On Fri, 04 May 2018 14:38:54 -0600, Ian Kelly wrote:
> On Fri, May 4, 2018 at 11:04 AM, Steven D'Aprano
> wrote:
[...]
>> My guess is that they were thinking that there's no need to complicate
>> itemgetter for this use-case when it is just as easy to write up a
>> quick lambda to do the job.
>
On Fri, 04 May 2018 15:27:02 +0200, Thomas Jollans wrote:
spamgetter = (lambda seq, i=2, fallback="spam":
> ... seq[i] if abs(i) < len(seq) or i == -len(seq)
> ... else fallback)
spamgetter("abcd", i=-4)
> 'a'
spamgetter("abcd")
> 'c'
spamgett
On Fri, 04 May 2018 15:27:16 +0200, Antoon Pardon wrote:
>> I might be slow today, but I cannot see how to write a clear, obvious,
>> efficient lambda that provides functionality equivalent to itemgetter
>> with a default value.
[...]
> This seems to work:
>
> f = (lambda seq: (list(seq) + 3 * [
On 04/05/18 22:38, Ian Kelly wrote:
> On Fri, May 4, 2018 at 11:04 AM, Steven D'Aprano
> wrote:
>> On Fri, 04 May 2018 09:17:14 -0600, Ian Kelly wrote:
>>
>>> On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano
>>> wrote:
Here are the specifications:
* you must use lambda, not def;
>>>
On Fri, May 4, 2018 at 11:04 AM, Steven D'Aprano
wrote:
> On Fri, 04 May 2018 09:17:14 -0600, Ian Kelly wrote:
>
>> On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano
>> wrote:
>>> Here are the specifications:
>>>
>>> * you must use lambda, not def;
>>
>> Why? This seems like an arbitrary constraint
On Fri, 04 May 2018 09:17:14 -0600, Ian Kelly wrote:
> On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano
> wrote:
>> Here are the specifications:
>>
>> * you must use lambda, not def;
>
> Why? This seems like an arbitrary constraint.
You'll have to ask the two core devs. In my post, in the part y
On Sat, May 5, 2018 at 1:17 AM, Ian Kelly wrote:
> On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano
> wrote:
>> Here are the specifications:
>>
>> * you must use lambda, not def;
>
> Why? This seems like an arbitrary constraint.
>
> def itemgetter2(*items, default):
> return lambda seq: tuple(
On Fri, May 4, 2018 at 7:01 AM, Steven D'Aprano
wrote:
> Here are the specifications:
>
> * you must use lambda, not def;
Why? This seems like an arbitrary constraint.
> * the lambda must take a single function, the sequence you want to
> extract an item from;
>
> * you can hard-code the index
On 04-05-18 15:01, Steven D'Aprano wrote:
> A re-occurring feature request is to add a default to itemgetter and
> attrgetter. For example, we might say:
>
> from operator import itemgetter
> f = itemgetter(1, 6, default="spam") # proposed feature
> f("Hello World!") # returns ('e', 'W')
> f("He
On 2018-05-04 15:01, Steven D'Aprano wrote:
> A re-occurring feature request is to add a default to itemgetter and
> attrgetter. For example, we might say:
>
> from operator import itemgetter
> f = itemgetter(1, 6, default="spam") # proposed feature
> f("Hello World!") # returns ('e', 'W')
> f(
19 matches
Mail list logo