[Python-ideas] Does jargon make learning more difficult?

Abe Dillon abedillon at gmail.com
Tue Aug 21 14:56:16 EDT 2018


[Chris Angelico]

> 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.


The revelation that it's a function should come when you read the "by" or
"key". If you don't know what that parameter is, then that's where the
"wait wait wiat!" should happen. Then type "help(sorted)" in a console or
something. Another hint can come from the appearance of an undeclared
variable: card. (I know, I know: I didn't declare several variables, just
imagine that 'card' is the only variable not previously declared).

It's no more surprising than reading a comprehension:

initials = {person.name[0] for person in people if person.name}

initials = # we're assigning this to the name "initials"
initials = { # it's a dictionary or set of something
initials = { person.name[0]  # I'v never seen "person" before, this must be
a comprehension or something
                             # anyway, no ":" means it's a set of the first
letter of each person's name
intiials = { person.name[0] for person # WAIT WAIT WIAT WHAT?!? Just
kidding, I saw this coming...

[Chris Angelico]

> > 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.


If the logic is simple enough, you can just use a reference to the method
from the class:

hand = sorted(cards, by=Card.suit)  # where Card is the class

That's why I tend to use a more complex example. Otherwise the lambda isn't
really justified.

[Chris Angelico]

> JFTR, I wouldn't implement a deck of cards this way.


I don't implement decks of cards often, but in toy problems; I find just
using 2-character strings works fine:

Ace_of_spades = "As"
Ten_of_hearts = "Th"
etc...

That can easily be extended by a named tuple initialized with unpacking:

Card = namedtuple("Card", "rank suit")
Ace_of_spades = Card(*"As")

[Chris Angelico]

> It implies that there is exactly one wild card, where many
> decks of cards have at least two.


I don't know of many games that have wild cards, but that's a simple remedy:

hand = sorted(cards, by=value[card.suit] if card not in wilds else
max_value with card)

[Chris Angelico]

> 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.


I promise that wasn't my intent. Since both my proposed form and the lambda
form use the same expression,
it doesn't really tip the balance in favor of my argument. Also, most toy
card problems I work with use a finite,
immutable set of cards, so identity checking isn't *that* weird.

On Tue, Aug 21, 2018 at 1:12 PM, Chris Angelico <rosuav at gmail.com> wrote:

> On Wed, Aug 22, 2018 at 3:56 AM, Abe Dillon <abedillon at 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 at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180821/1f587612/attachment-0001.html>


More information about the Python-ideas mailing list