Coding standard: Prefixing variables to indicate datatype

Terry Reedy tjreedy at udel.edu
Fri Jan 17 12:31:01 EST 2003


"Simon Burton" <simonb at webone.com.au> wrote in message
news:pan.2003.01.17.09.34.53.724229 at webone.com.au...
> > I always use string.join because ''.join feels counter-intuitive,
> > because you're joining list. Sort of like saying 'bank maria went
to'.
> And he said:
> "Empty string, exercise your ability to join this list."

These statements are misleading.  The argument to .join can be *any*
sequence of strings or iterable that produces such.  Strings
themselves, tuples (of strings),
iterators (that yield strings), string-keyed dicts(!), etc; not just
lists.

>>> '.'.join(('1','2','3'))
'1.2.3'
>>> '.'.join('abc')
'a.b.c'
>>> from __future__ import generators
>>> def g():
...   for i in range(5): yield str(i)
...
>>> '.'.join(g())
'0.1.2.3.4'
>>> '.'.join({'a':1,'b':2})
'a.b'


On the other hand, join only applies to lists of *strings* and not
lists (or sequences) in general.

> Ya, not obvious,

Once you actually think about it, its obvious that attaching the join
method to the joiner is the only sensible place to put it.

"Carpenter, exercise your ability to joins these pieces of wood."

Terry J. Reedy






More information about the Python-list mailing list