meaning of [ ]

Rustom Mody rustompmody at gmail.com
Mon Sep 4 13:37:37 EDT 2017


On Monday, September 4, 2017 at 10:42:47 PM UTC+5:30, Rick Johnson wrote:
> On Monday, September 4, 2017 at 9:27:23 AM UTC-5, Rustom Mody wrote:
> > On Monday, September 4, 2017 at 6:36:11 PM UTC+5:30, Ben Bacarisse wrote:
> > > Rustom Mody  writes:
> > > 
> > > > On Sunday, September 3, 2017 at 5:10:13 PM UTC+5:30, Rick Johnson wrote:
> > > >> Andrej Viktorovich wrote:
> > > >> > I suppose p becomes array of strings but what [] means in this statement?
> > > >> 
> > > >> Generally, it's an inline form of writing a loop that returns a
> > > >> list. There are other types as well.
> > > >
> > > > Tsk tsk the confusioning continues
> > > >
> > > > Rewrite
> > > > [p for p in sys.path] 
> > > > as
> > > > [p | p ∈ sys.path]
> > > >
> > > > Is that clearer?
> > > >
> > > > And then as
> > > >
> > > > {p | p ∈ sys.path}
> > > > And refresh the idea of set-builder notation
> > > > http://www.mathwords.com/s/set_builder_notation.htm
> > > 
> > > But [p for p in sys.path] is a list and "set-builder" notation is used
> > > for sets.  Order is crucial for sys.path.  You say exactly that below so
> > > I don't see how referring to sets helps anyone understand lists.
> > 
> > Clearly if the question was of *sets* vs *lists* the
> > distinction is at least as crucial, maybe even more so than
> > the similarity.
> 
> What made you assume the OP question had anything to do with
> sets versus lists? I didn't get that impression. Not to say
> that i could not be wrong, but i see no reason to believe i
> am.

I assume you are addressing this to Ben Bacarisse.
If to me then I am saying mostly what you are (in above para!)

> 
> > The OP-question here however is one about comprehensions
> > and it appears without the questioner realizing that — as
> > Peter's answer showed
> 
> My understanding of the impetus of the OP's question is a
> simple matter of attempting to intuit the structure of a
> list comprehension in the realms of Python code. Not a
> question as to the supposed "impurity" of python list
> comprehension form compared with set notation form. But
> again, i could be wrong.


My understanding is that the OP saw a 'for' inside a '[]' and wondered
"WTF is this?"



> 
> > See the very first line of this:
> > https://en.wikipedia.org/wiki/List_comprehension
> > “List comprehension follows the form of the mathematical
> > set-builder notation (set comprehension)”
> 
> I'm not sure if we should consider Wikipedia an "official"
> definition of the Python language, but if it were up to me,
> i would have chosen my words more carefully, as in: "List
> comprehension follows _loosely_ the form of the mathematical
> set-builder notation (set comprehension)”. My understanding
> that list comprehensions were borrowed from other languages.

Wikipedia is probably not the best definer for math either
In any case here its a question of how and what is the link between
a math concept/notation and its form/semantics in python

> 
> > ie its both historically and structurally linked IOW
> > emphasising the incidental sequential nature of the
> > computation at the cost of the fundamental structure-
> > preserving nature of the concept
> > > 
> > > <snip>
> > > > As Peter pointed out this is a no-op
> > > > ie
> > > > [p for p in sys.path] 
> > > >
> > > > could be written as
> > > > list(sys.path)
> > > 
> > > Both make a copy -- that's not a no-op.  It may be a very-little-op but
> > > not nothing.
> > 
> > 
> > Its important…
> > - whether the machine stack grows up or down
> > - whether the bytes are ordered little-endian or big-endian
> > - whether IO is polled or interrupt driven
> > - whether IO ports are memory-mapped or in separate IO space
> > And much else
> > 
> > Yet for most people doing most things today in most languages,
> > these questions are irrelevated
> 
> Of course. Just as pointers and memory management are
> irrelevant...

careful! In an ideal world they should be irrelevant
Some knotty problems in python cannot be prized apart without talking of these

> 
> > The question of copying is likewise, from a certain point
> > of view, irrelevant.
> > 
> > Of course the further context of the OP may have a mutation
> > on the list And if he does end up changing sys.path, and
> > not intending it, that would be a source of nasty bugs for
> > sure But why assume all that when he does not (yet) know
> > what it is he is writing More importantly thinks its a
> > *command* when its actually an *expression* etc? (IMHO an
> > unfortunate consequence of the confusing overloading of the
> > 'for' keyword)
> 
> I agree that if one is to present an "example
> comprehension", then using sys.path as the target is
> probably not a good idea. Yes, [p for p in sys.path] is
> superfluous, but so too is [i for i in range(5)]. But in all
> my life, i've never heard anyone raise even a slight quibble
> over the superfluous nature of such academic examples. (if
> that's what this was?)

Firstly [i for i in range(5)] is not a no-op in python-3 where
range returns a range-object. list(range(5)) may be preferred for succinctness
However [i for i in range(5)] may be said to be more general
> 
> > > 
> > > > [Not sure why he didnt say just sys.path]
> > > 
> > > Because he wanted code equivalent to [p for p in sys.path].
> > > 
> > > > Anyway this is a good example to distinguish
> > > >
> > > > [p for p in sys.path] 
> > > > from
> > > > {p for p in sys.path}
> > > >
> > > > Both work in python
> > > > But the second is probably not correct because path-searching is order
> > > > dependent
> > > 
> > > Right.  So i'm puzzled why you suggest that [p for p in sys.path] should
> > > be understood by reading about set-builder notation.
> > 
> > Since "-builder" and "-comprehension" (in this context) are synonymous??
> > [ https://en.wikipedia.org/wiki/Set-builder_notation ]
> > 
> > And I am ready to make a small wager 
> > - that the OP knows of these
> > - with math-hat on
> > - And is simply confused by the clunky ASCII syntax
> 
> "clunky ASCII syntax"? 
> 
> WTH???
> 
> Oh, i get it!
> 
> Are you on some sort of "set notation purity" jihad?

No purity (Dunno about jihad)
Just that when a notation like python's comprehensions are a close replica of
a much more widely known older notion/notation from somewhere (in THIS case math)
drawing attention to that history can help someone make the link

Emacs is a good example of how to do all this wrong.
Emacs calls:

windows as frames
panes as windows
Alt as Meta
And much else that is gratuitously non-standard

May be there for some reason, historical or otherwise
But is nonetheless rather off-putting to any noob

Likewise here:
People prefer to give the implementation of comprehensions 
rather than giving the connection to its parent notion+notation which in
all probability the OP knows and is simply unable to connect because of 
clunkyness of ASCII-fied syntax



More information about the Python-list mailing list