Explanation of this Python language feature? [x for x in x for x in x] (to flatten a nested list)

Steven D'Aprano steve at pearwood.info
Tue Apr 8 04:21:26 EDT 2014


On Mon, 07 Apr 2014 20:33:31 -0500, Mark H Harris wrote:

>     I have another question for y'all, is a function (particularly a
> generator) a noun or a verb?  

Mu.

http://en.wikipedia.org/wiki/Mu_%28negative%29#.22Unasking.22_the_question


Nouns and verbs are concepts from a completely different magisteria, 
namely linguistics. Functions are no more a noun (or verb) than list 
comprehensions are adjectives.

What we can say is that in Python, functions are objects, and being 
objects, they are also values, like ints, strings, lists, floats, etc. 
Even in languages where functions are not first-class values, e.g. 
Pascal, we treat them as abstract things rather than actions, so 
linguistically we use functions as nouns. E.g. given a function "spam", 
we might say "pass the argument to spam" rather than "spam that argument".

We do that even when the function is named for a verb: "pass the argument 
to execute". (English is great for this: we can use nearly every verb as 
a noun, if the context is understood.)


> Does a function (or generator) 'do'
> something (based on name and parms) or does it 'return' something based
> on name and parms? 

Both. Returning something is just a special case of doing. Monkeys climb, 
fish swim, cows moo, functions return, and programmers drink caffeinated 
drinks.


> Based on name and parms should a function (or
> generator) function as a noun, or function as a verb, or *both*? --or,
> are Classes nouns only, and all functions *are* verbs only?

I *think* you are referring to naming conventions here.

Functions which are intended to be used as a procedure, that is, only for 
their side-effects, should be named using a verb:

time.sleep
dict.update
list.append
Spam.make_yummy_foodstuffs

Functions which are intended to return a value may be named as verbs:

run
eval
sorted
print

or as nouns:

int
str
dict
namedtuple
coordinate
array


(the first three are now types, i.e. classes, but early in Python they 
were functions).

Classes represent things (possible abstract things), and so should be 
named as nouns, not verbs:

Runner not Run or Do_Run
Decimal not Decimalize or Do_Decimal
float not Make_Floating_Point


Generator functions are called for their value: a generator function 
returns a generator, and a generator is a value:

def make_generator(n):
    for i in range(n):
        yield "something"

gen = make_generator(23)


Since the generator object itself is a thing, it should be named with a 
noun. Since the generator function is also a thing, and it is called for 
it's return value, not a side-effect, it could be named as a verb or a 
noun, whichever you prefer, or makes sense in context.

Are there "things" in Python that aren't values? No. But there is syntax 
that represents verbs:

import this
del that
for <something> [do]: this
while condition [do]: that

There's no such thing in Python as an "Import object" or a "DelType 
value", but Python provides verbs for those commands.



-- 
Steven



More information about the Python-list mailing list