[Python-ideas] Add list.join() please
chris.barker at noaa.gov
Tue Jan 29 15:49:19 EST 2019
A couple notes:
On Tue, Jan 29, 2019 at 5:31 AM Jamesie Pic <jpic at yourlabs.org> wrote:
> can you clarify the documentation
> topic you think should be improved or created ? "Assembling strings"
I would think "assembling strings", though there is a lot out there already.
> or "inconsistencies between os.path.join and str.join" ?
well, if we're talking about moving forward, then the Path object is
probably the "right" way to join paths anyway :-)
a_path / "a_dir" / "a_filename"
But to the core language issue -- I started using Python with 1.5.* and
back then join() was in the string module (and is there in 2.7 still)
And yes, I did expect it to be a list method...
Then it was added as a method of the string object.
And I thought THAT was odd -- be really appreciated that I didn't need to
import a module to do something fundamental.
But the fact is, that joining strings is fundamentally a string operation,
so it makes sense for it to be there.
In earlier py2, I would have thought, maybe it should be a list method --
it's pretty darn common to join lists of strings, yes? But what about
tuples? Python was kind of all about sequences -- so maybe all sequences
could have that method -- i.e part of the sequence ABC.
But with > py3k, Python is more about iterables than sequences -- and join
(and many other methods and functions) operate on any iterable -- and this
is a really good thing.
So add join to ALL iterables? That makes little sense, and really isn't
possible -- an iterable is something that conforms to the iterator protocol
-- it's not a type, or even an ABC.
So in the end, join really does only make sense as string method.
Or Maybe as a built in -- but we really don't need any more of those.
If you want to argue that str.join() should take multiple arguments, like
os.path.join does, then, well we could do that -- it currently takes one
and only one argument, so it could be extended to join multiple arguments
-- but I have hardly ever seem a use case for that.
The mistake I'm still doing after 10 years of Python
hmm -- I've seen a lot of newbies struggle with this, but haven't had an
issue with it for years myself.
> >>> '/'.join('some', 'path')
> TypeError: join() takes exactly one argument (2 given)
pathlib aside, that really isn't the right way to join paths .....
os.path.jon exists for a (good) reasons. One of which is this:
In : os.path.join("this/", "that")
Christopher Barker, Ph.D.
Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-ideas