Skip to content
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

Closed
lopgok mannequin opened this issue Dec 4, 2008 · 9 comments
Closed

problem with str.join - should work with list input, error says requires 'str' object #48784

lopgok mannequin opened this issue Dec 4, 2008 · 9 comments
Labels
type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@lopgok
Copy link
Mannequin

lopgok mannequin commented Dec 4, 2008

BPO 4534
Nosy @amauryfa

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:

assignee = None
closed_at = <Date 2008-12-05.00:25:13.631>
created_at = <Date 2008-12-04.20:34:22.152>
labels = ['invalid', 'type-crash']
title = "problem with str.join - should work with list input, error says requires 'str' object"
updated_at = <Date 2013-07-13.10:11:23.836>
user = 'https://bugs.python.org/lopgok'

bugs.python.org fields:

activity = <Date 2013-07-13.10:11:23.836>
actor = 'bkyasi'
assignee = 'none'
closed = True
closed_date = <Date 2008-12-05.00:25:13.631>
closer = 'amaury.forgeotdarc'
components = ['None']
creation = <Date 2008-12-04.20:34:22.152>
creator = 'lopgok'
dependencies = []
files = []
hgrepos = []
issue_num = 4534
keywords = []
message_count = 9.0
messages = ['76924', '76935', '76938', '76939', '76952', '76967', '76980', '77092', '193009']
nosy_count = 4.0
nosy_names = ['amaury.forgeotdarc', 'LambertDW', 'lopgok', 'bkyasi']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue4534'
versions = ['Python 3.0']

@lopgok
Copy link
Mannequin Author

lopgok mannequin commented Dec 4, 2008

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.

@lopgok lopgok mannequin added the type-crash A hard crash of the interpreter, possibly with a core dump label Dec 4, 2008
@lopgok lopgok mannequin changed the title problem with str.join problem with str.join - should work with list input, error says requires 'str' object Dec 4, 2008
@lambertdw
Copy link
Mannequin

lambertdw mannequin commented Dec 4, 2008

Try this---

def List_to_String(lis,separator=''):
    return separator.join(lis)

@lopgok
Copy link
Mannequin Author

lopgok mannequin commented Dec 4, 2008

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.

@lambertdw
Copy link
Mannequin

lambertdw mannequin commented Dec 4, 2008

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.

@amauryfa
Copy link
Member

amauryfa commented Dec 5, 2008

>> str.join(lis, '')

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

>> import string
>> string.join(lis, '')

@lopgok
Copy link
Mannequin Author

lopgok mannequin commented Dec 5, 2008

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?

@amauryfa
Copy link
Member

amauryfa commented Dec 5, 2008

"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)

@lopgok
Copy link
Mannequin Author

lopgok mannequin commented Dec 6, 2008

I fixed the code as follows:

return str.join('',lis)

I think it is readable, and I understand it.

Thanks everyone for clarifying everything

@bkyasi
Copy link
Mannequin

bkyasi mannequin commented Jul 13, 2013

it is really a good help

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

1 participant