This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: problem with str.join - should work with list input, error says requires 'str' object
Type: crash Stage:
Components: None Versions: Python 3.0
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: LambertDW, amaury.forgeotdarc, bkyasi, lopgok
Priority: normal Keywords:

Created on 2008-12-04 20:34 by lopgok, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (9)
msg76924 - (view) Author: jeff deifik (lopgok) Date: 2008-12-04 20:34
I compiled python 3.0 on a cygwin platform.

Here is my modest function:

def List_to_String(lis):
#    return str.join(lis, '')		# This is fast, but seems broke in 3.0
    s = ''				# This is really slow, but works in 3.0
    for l in lis: s = s + l
    return s

Here is my test case:
    def test_List_to_String(self):
        inp = ['f', 'r', 'e', 'd', ' ', 'i', 's']
        out = 'fred is'
        self.assertEqual(jefflib.List_to_String(inp), out)

Here is what happens when I try to run the commented out version (the
one with the join):
ERROR: test_List_to_String (__main__.TestJefflibFunctions)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./jefflib_test.py", line 96, in test_List_to_String
    self.assertEqual(jefflib.List_to_String(inp), out)
  File "/cygdrive/c/documents and
settings/deifikj/jeff/scripts/jefflib.py", lin
e 256, in List_to_String
    return str.join(lis)
TypeError: descriptor 'join' requires a 'str' object but received a 'list'


Of course, it worked fine in python 2.6.
I am baffled.
msg76935 - (view) Author: David W. Lambert (LambertDW) Date: 2008-12-04 22:30
Try this---

def List_to_String(lis,separator=''):
    return separator.join(lis)
msg76938 - (view) Author: jeff deifik (lopgok) Date: 2008-12-04 22:40
Thanks.
I want to learn what is wrong with the code I have though.
My main goal is to understand python 3.0 better, rather than
fixing a specific problem.
msg76939 - (view) Author: David W. Lambert (LambertDW) Date: 2008-12-04 22:44
I did this to find out what are str.join's arguments---

$ python3 -c 'help(str.join)'

Help on method_descriptor:

join(...)
    S.join(sequence) -> str
    
    Return a string which is the concatenation of the strings in the
    sequence.  The separator between elements is S.
msg76952 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-12-05 00:25
>>> str.join(lis, '')

I doubt this really worked with 2.6.
Wasn't it something like:

>>> import string
>>> string.join(lis, '')
msg76967 - (view) Author: jeff deifik (lopgok) Date: 2008-12-05 02:00
Yes, it was
 import string
 string.join(lis, '')

I am still a bit confused though.
Why doesn't my code of str.join(lis, '') work?

I don't think str is a reserved word.
I suppose that python might be able to deduce that str is of type
string, based on join being called on it.
Is the problem that python thinks my str is of type list?
msg76980 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-12-05 08:13
"str" is not your string, it's not a string; "str" is a class name, a
standard type.

"str.join" is a unbound method. You can call it directly if you pass an
actual string object in front of the other arguments.
But the normal way to call methods is to use them on objects:

someString = ""
someList = ['f', 'r', 'e', 'd', ' ', 'i', 's']
someString.join(someList)
msg77092 - (view) Author: jeff deifik (lopgok) Date: 2008-12-06 01:57
I fixed the code as follows:

return str.join('',lis)

I think it is readable, and I understand it.

Thanks everyone for clarifying everything
msg193009 - (view) Author: arsalan (bkyasi) Date: 2013-07-13 10:11
it is really a good help
History
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48784
2013-07-13 10:11:23bkyasisetnosy: + bkyasi
messages: + msg193009
2008-12-06 01:57:34lopgoksetmessages: + msg77092
2008-12-05 08:13:59amaury.forgeotdarcsetmessages: + msg76980
2008-12-05 02:00:20lopgoksetmessages: + msg76967
2008-12-05 00:25:13amaury.forgeotdarcsetstatus: open -> closed
resolution: not a bug
messages: + msg76952
nosy: + amaury.forgeotdarc
2008-12-04 22:44:29LambertDWsetmessages: + msg76939
2008-12-04 22:40:47lopgoksetmessages: + msg76938
2008-12-04 22:30:45LambertDWsetnosy: + LambertDW
messages: + msg76935
2008-12-04 20:35:14lopgoksettitle: problem with str.join -> problem with str.join - should work with list input, error says requires 'str' object
2008-12-04 20:34:22lopgokcreate