meaning of [ ]

Rustom Mody rustompmody at gmail.com
Mon Sep 4 10:27:01 EDT 2017


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.
The OP-question here however is one about comprehensions and it appears without
the questioner realizing that — as Peter's answer showed

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)”
 
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

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)

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



More information about the Python-list mailing list