[Edu-sig] Using Grammar with OO
kirby urner
kirby.urner at gmail.com
Thu May 14 20:32:30 CEST 2015
> In the help documentation, the grammatical
> concepts of noun, verb, adverb and more, get used
> to introduce the grammar of J. That seems effective
> and I've been using something like this with OO
> (not Python-specific):
>
> noun.adjective = value
> noun.verb( )
>
Continuing with this theme, I also find it important
to draw attention to the ( ) after the verb, given
Python lets you mention noun.verb by name without
calling it, an important distinction when writing callbacks.
"The grammar of __call__" we could call it, following
the practice of underlining the partly-J-inspired
grammatical approach in the help docs.
Here, the primitive association of "emoticon mouth"
:-() <-- i.e. "mouth sideways"
with "callable mouth" () e.g. __call__(), is important.
To say something is "callable" is to say it "has a
mouth" (in principle, even if it needs no arguments,
either positional or keyword).
That's the signature of a verb( ) -- which is any
callable, not just "functions". The types themselves
are callables e.g. enumerate, and range.
"Has a mouth" is a concept like "has a face" i.e.
it translates somewhat universally across human
languages.
None of the Python keywords have mouths i.e.
keywords are not callables. When print went from
being a keyword in Python 2.x to a function in
Python 3.x, it only then became a callable.
Beginner Python coders are apt to think of return
or perhaps if, as callables:
if(True):
return(True)
they will say, as they're thinking of if and return as
callables (a mistake). The thing is, the syntax is
still legal because
return x
return (x)
return ((x))
return (((x)))
...
all mean the same thing. The same confusions
arise with print. print(3) works until you come to
python(3, 4, sep="") with a Python 2.x interpreter:
that's when the exception gets raised. Not until
a keyword argument is mentioned.
So just saying "return has no mouth" and/or
"print has a mouth now, in newer Pythons" may
be the better mnemonic to use.
Kirby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20150514/5cb4ffb6/attachment.html>
More information about the Edu-sig
mailing list