On Sun, Mar 5, 2017 at 4:51 AM, Ed Kellett <edk...@gmail.com> wrote: > It's hard to show examples because, generally speaking, when one can't do > a thing one does something else instead. I can restructure my programs to > avoid having this problem, or, if I'm in a hurry, I can use one of the many > ugly h^W^Wobvious, simple and elegant solutions, like (args[2:3] + ["<No > reason given>"])[0]. > > For the record, even though I was the first in this thread to give that spelling, I don't think it's the best way to spell it in current Python. Bracketing a helper function (which *could* after all deal with lists, dicts, and whatever other type you wanted depending on what you implement), I think the best spelling is with a ternary:
reason = args[1] if len(args)>1 else "<No reason given>" The more the thread continues the more I actively want to avoid a list.get() method. Initially I thought it added symmetry; but as I look at it I realize it is mostly code smell. Being able to get "a value" from an arbitrary position in a list that isn't long enough often suggests something is deeply wrong in the logic of the code. Moreover, it is likely to let bugs pass silently and cause deeper problems elsewhere downstream. If you have a list that is expected to have a length of either 1 or 2, I can imagine this making sense (e.g. ["kill"] vs. ["kill", "edk"]): reason = args.get(1, "<No reason given>") But if the next line is: data = args.get(17, "<empty>") Then I'm pretty sure the programmer thinks she's being passed a very different type of collection than is actually available. I'd rather that fails right away and in an obvious way then silently produce a value. Specifically, if I think I'm dealing with a list that is likely to have 20 items (rather than maybe 4 or fewer), I'm almost sure the best way to deal with it is in a list (or comprehension, map(), etc) and NOT by poking into large index positions that may or may not be present. -- Keeping medicines from the bloodstreams of the sick; food from the bellies of the hungry; books from the hands of the uneducated; technology from the underdeveloped; and putting advocates of freedom in prisons. Intellectual property is to the 21st century what the slave trade was to the 16th.
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/