5

The following example shows the error I am getting when trying to use a string function inside a function call to map. I need help with why this happening. Thanks.

>>> s=["this is a string","python python python","split split split"]
>>> map(split,s)
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    map(split,s)
NameError: name 'split' is not defined

though split() is an in-built function, but it still throws this error?

2 Answers 2

14

It will work fine if you use str.split()

I.e,

s = ["this is a string","python python python","split split split"]
map(str.split, s)

gives:

[['this', 'is', 'a', 'string'],
 ['python', 'python', 'python'],
 ['split', 'split', 'split']]

The error message states: NameError: name 'split' is not defined, so the interpreter doesn't recognize split because split is not a built-in function. In order for this to work you have to associate split with the built-in str object.

Update: Wording improved based on @Ivc's helpful comments/suggestions.

Sign up to request clarification or add additional context in comments.

18 Comments

It isn't to disambiguate, but rather because its a string method - 'blah'.split('a') is equivalent to str.split('blah', 'a'), same as instanceofmyclass.mymethod(arg) is equivalent to MyClass.mymethod(instanceofmyclass, arg) - the string is the self argument to str.split.
This is kind of ugly. I'd rather call the method like a method, e.g. map(lambda x: x.split(), s), or better, just go [x.split() for x in s]
@AshwiniChaudhary exactly, that's it. Since we quite frequently attach split to a string it's usually clear which split we mean.
@isbadawi you downvoted because of aesthetics?? I didn't chose this code construct. My posting is in direct response to what the OP posted and answers exactly his concern. If you don't like his code fine, but why downvote my answer for explaining the problem he asked about? He didn't ask for an alternative code construct but an explanation why his code didn't work. I explained this correctly I believe. Your comment to me above really ought to be a comment to the OP at the top of the page.
@isbadawi My answer already clearly states that split is not a built-in. I am not responsible for what you consider "hacky and unpythonic" .. these comments ought to be directed at OP. You also could have posted your own answer (why don't you?). I don't care about the 2 points, more the reasoning which makes no sense to me as I explained above, so there's no point in further discussion.
|
2

split is not a built-in function, though str.split is a method of a built-in object. Generally, you would call split as a method of a str object, so that's why it's bound directly.

Check the output from the interpreter:

>>> str
<type 'str'>
>>> str.split
<method 'split' of 'str' objects>

2 Comments

I don't believe that is correct (str.stplit being a built-in function). It is not listed here: docs.python.org/library/functions.html, and cleary associated with str, so a method.
@Jeff I changed it to that for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.