New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
problem with str.join - should work with list input, error says requires 'str' object #48784
Comments
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: Here is what happens when I try to run the commented out version (the 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. |
Try this--- def List_to_String(lis,separator=''):
return separator.join(lis) |
Thanks. |
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. |
I doubt this really worked with 2.6.
|
Yes, it was I am still a bit confused though. I don't think str is a reserved word. |
"str" is not your string, it's not a string; "str" is a class name, a "str.join" is a unbound method. You can call it directly if you pass an someString = "" |
I fixed the code as follows: return str.join('',lis) I think it is readable, and I understand it. Thanks everyone for clarifying everything |
it is really a good help |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: