> > As attrgetter/itemgetter might get heterogeneus data, I would expect a > per-get default, not [just] a global default. > +1
Perhaps something like: > > >>> itemgetter(-1, 0, -2, > ... default0="first argument default", > ... default1=["second", "argument", "default"], > ... default={"global": "default"}, > ... ) > The keywords suffixed by indice are uncommon. Maybe we can use dedicated object. >>> itemgetter( ... itemgetter.WithDefault(-1, "first argument default"), ... itemgetter.WithDefault(0, ["second", "argument", "default"]) ... -2 # has no default value ... ) Another possibility is that itemgetter becomes an object with `add_default` method. >>> getter = itemgetter(-1, 0, 2) >>> getter.add_default(0, "first argument default") >>> getter.add_default(2, "third argument default") or simply default parameter should be a list >>> itemgetter(-1, 0, 2, ... default=["first", itemgetter.NoDefault, "third"]) 2018-05-08 1:37 GMT+02:00 Danilo J. S. Bellini <danilo.bell...@gmail.com>: > (1) > > On 7 May 2018 at 01:07, Raymond Hettinger <raymond.hettin...@gmail.com> > wrote: > >> He could've written: >> >> get = rpartial(getattr, 'foo', None) >> return get(args) or get(config) or get(env) >> > > That's somewhat hybrid, it would behave like: > > lambda obj: getattr(obj, "foo", None), > > but one might expect a "reversed partial" to have its arguments reversed > as well, like: > > lambda obj: getattr(obj, None, "foo"). > > (2) > > Why getattr can't accept keyword arguments? > > >>> getattr_zero = partial(getattr, default=0) > >>> getattr_zero({}, "blah") > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > TypeError: getattr() takes no keyword arguments > > Since partial/partialmethod can apply keyword arguments, allowing the > "default=" keyword argument in getattr would be an alternative to the > "rpartial". > > (3) > > At one time, lambda was the one obvious way. [...] >> > > I really like lambdas, but there are some "closure gotchas" like: > > >>> for idx in indices: > ... do_something(lambda seq: seq[idx]) > > If the lambda is stored somewhere to be called after the loop ends (or > after its iteration), the seq[idx] would load the item with the last > iterated index. This: > > >>> for idx in indices: > ... do_something(itemgetter(idx)) > > would behave more like this instead: > > >>> for idx in indices: > ... do_something((lambda i: (lambda seq: seq[i]))(idx)) > > Which I wouldn't call "the one obvious way". > > (4) > > If itemgetter and attrgetter only did a single lookup, a default might >> make sense. However, that doesn't fit well with multiple and/or chained >> lookups where are number of options are possible. (See >> https://bugs.python.org/issue14384#msg316222 for examples and >> alternatives). > > As attrgetter/itemgetter might get heterogeneus data, I would expect a > per-get default, not [just] a global default. > Perhaps something like: > > >>> itemgetter(-1, 0, -2, > ... default0="first argument default", > ... default1=["second", "argument", "default"], > ... default={"global": "default"}, > ... ) > > -- > Danilo J. S. Bellini > --------------- > "*It is not our business to set up prohibitions, but to arrive at > conventions.*" (R. Carnap) > > _______________________________________________ > Python-ideas mailing list > Python-ideas@python.org > https://mail.python.org/mailman/listinfo/python-ideas > Code of Conduct: http://python.org/psf/codeofconduct/ > >
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/