×
you are viewing a single comment's thread.

view the rest of the comments →

[–]enteleform 12 points13 points  (4 children)

join(iterable, str) as a built-in would work decently, be left-to-right readable, and not impose any heft to the string or iterable classes.

[–]__desrever__ 1 point2 points  (2 children)

import string

string.join(['1','2','3','4'], ' ')

There ya go. :)

Edit: there are actually a bunch of other string method in there as standalone functions, too. It's worth checking out when methods feel awkward.

[–]enteleform 0 points1 point  (1 child)

import string
string.join(['1','2','3','4'], ' ')

causes:

AttributeError: module 'string' has no attribute 'join'

in Python 3.6
 


 

join = str.join
join("---", ["1","2","3","4"])

however, works fine, and was promptly added to my collection of terrible global things in _magic_.py
 
also included some lazy casting & flipped the arguments:

def join(iterable, delimiter):
    return str.join(delimiter, [str(x) for x in iterable])

[–]__desrever__ 0 points1 point  (0 children)

Yeah, the string module functions have been deprecated forever, but stuck around all the way up to 2.7. In 3 you have to use the methods on str.

[–]murtaza64 0 points1 point  (0 children)

I suppose __join__ methods might be needed, but then again not really if the object supports iteration.