[Python-Dev] sum(...) limitation
Terry Reedy
tjreedy at udel.edu
Mon Aug 11 22:29:03 CEST 2014
On 8/11/2014 8:26 AM, Ben Hoyt wrote:
> It seems to me this is something of a pointless discussion -- I highly
> doubt the current situation is going to change, and it works very well.
> Even if not perfect, sum() is for numbers, sep.join() for strings.
> However, I will add one comment:
>
> I'm overall -1 on trying to change the current situation (except for
> adding a join() builtin or str.join class method).
>
>
> Did you know there actually is a str.join "class method"?
A 'method' is a function accessed as an attribute of a class.
An 'instance method' is a method whose first parameter is an instance of
the class. str.join is an instance method. A 'class method', wrapped as
such with classmether(), usually by decorating it with @classmethod,
would take the class as a parameter.
> I've never
> actually seen it used this way, but for people who just can't stand
> sep.join(seq), you can always call str.join(sep, seq) -- works in Python
> 2 and 3:
>
> >>> str.join('.', ['abc', 'def', 'ghi'])
> 'abc.def.ghi'
One could even put 'join = str.join' at the top of a file.
All this is true of *every* instance method. For instance
>>> int.__add__(1, 2) == 1 .__add__(2) == 1 + 2
True
However, your point that people who cannot stand the abbreviation
*could* use the full form that is being abbreviated.
In ancient Python, when strings did not have methods, the current string
methods were functions in the string module. The functions were removed
in 3.0. Their continued use in 2.x code is bad for 3.x compatibility,
so I would not encourage it.
>>> help(string.join) # 2.7.8
Help on function join in module string:
join(words, sep=' ')
join(list [,sep]) -> string
Return a string composed of the words in list, with
intervening occurrences of sep. The default separator is a
single space.
'List' is obsolete. Since sometime before 2.7, 'words' meant an
iterable of strings.
>>> def digits():
for i in range(10):
yield str(i)
>>> string.join(digits(), '')
'0123456789'
Of of the string functions, I believe the conversion of join (and its
synonum 'joinfields') to a method has been the most contentious.
--
Terry Jan Reedy
More information about the Python-Dev
mailing list