On Wed, Aug 22, 2018 at 3:56 AM, Abe Dillon <abedil...@gmail.com> wrote: > I've never hears someone say, "My child's name will be if it's a girl Sally > otherwise Billy."
"My child's name depends on gender - if it's a girl, Sally, otherwise Billy." Seems fine to me. You can always come up with something awkward in a particular order, but it's not the order that made it awkward. > Consider the alternate form: <expression> with <args> (thought there are > many alternative possibilities) > > hand = sorted(cards, key=lambda card: value[card.suit] if card is not wild > else max_value) > > hand = sorted(cards, by=value[card.suit] if card is not wild else > max_value with card) > > # notice how unsurprising it is that the signature is "card" Okay, let's read that. hand = # we're assigning this to the name 'hand' sorted( # calling the function named 'sorted' cards, # positional argument, whatever's in the 'cards' variable by= # keyword argument, what comes next is the 'by' argument value[card.suit] # subscript 'value' with 'card.suit' if card is not wild # yep else max_value # so we have an alternative with card # WAIT WAIT WAIT Once you get to 'with card', you have to go back and completely reinterpret everything prior to that as a function. You have to scan back and go "hang on, so exactly how much of this is getting wrapped up into a function here?". Contrast the Python version: hand = sorted(cards, key=lambda card: value[card.suit] if card is not wild else max_value) hand = # we're assigning this to the name 'hand' sorted( # calling the function named 'sorted' cards, # positional argument, whatever's in the 'cards' variable key= # keyword argument, what comes next is the 'key' argument lambda card: # a function taking one argument value[card.suit] # subscript 'value' with 'card.suit' if card is not wild # yep else max_value # so we have an alternative ) # and we're done The only part where you have to back up and change your interpretation is when you hit "if card is not wild", which reads well enough to justify the odd ordering. (JFTR, I wouldn't implement a deck of cards this way. It implies that there is exactly one wild card, where many decks of cards have at least two. In English, "card is not wild" can be interpreted as a membership check, but in Python, it is only an identity check; you're capitalizing on false readability by using this notation.) > Oh wait... Did I accidentally replace "key" with "by"? Huh... It seems to > make more sense even though the jargon is a "key function"... Oops! ;) I'm sure it would make even more sense if you wrote it this way: hand = sorted(cards, by="suit") That's how SQL works - you just name the column that you want to order the results by. But if you think "by" is a better keyword here, start explaining why your ordering is done with an anonymous function, not with an attribute name. ChrisA _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/