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.
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:42 | admin | set | github: 48784 |
| 2013年07月13日 10:11:23 | bkyasi | set | nosy:
+ bkyasi messages: + msg193009 |
| 2008年12月06日 01:57:34 | lopgok | set | messages: + msg77092 |
| 2008年12月05日 08:13:59 | amaury.forgeotdarc | set | messages: + msg76980 |
| 2008年12月05日 02:00:20 | lopgok | set | messages: + msg76967 |
| 2008年12月05日 00:25:13 | amaury.forgeotdarc | set | status: open -> closed resolution: not a bug messages: + msg76952 nosy: + amaury.forgeotdarc |
| 2008年12月04日 22:44:29 | LambertDW | set | messages: + msg76939 |
| 2008年12月04日 22:40:47 | lopgok | set | messages: + msg76938 |
| 2008年12月04日 22:30:45 | LambertDW | set | nosy:
+ LambertDW messages: + msg76935 |
| 2008年12月04日 20:35:14 | lopgok | set | title: problem with str.join -> problem with str.join - should work with list input, error says requires 'str' object |
| 2008年12月04日 20:34:22 | lopgok | create | |