meaning of [ ]

Rick Johnson rantingrickjohnson at gmail.com
Mon Sep 4 13:11:28 EDT 2017


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.

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

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

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

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

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

Heck, and here i am all this time, assuming that functional
programming was your only religion...

;-)




More information about the Python-list mailing list