{"id":588,"date":"2019-12-13T11:19:21","date_gmt":"2019-12-13T17:19:21","guid":{"rendered":"http:\/\/thenoyes.com\/littlenoise\/?p=588"},"modified":"2019-12-13T12:50:08","modified_gmt":"2019-12-13T18:50:08","slug":"using-map-with-class-methods","status":"publish","type":"post","link":"https:\/\/thenoyes.com\/littlenoise\/?p=588","title":{"rendered":"Using map with class methods &#8211; why map(split) doesn&#8217;t work"},"content":{"rendered":"<p>I have some sentences.<\/p>\n<pre>text = [\n  \"Call me Ishmael.\",\n  \"Some years ago, never mind how long precisely, having little or no money...\"\n]\n<\/pre>\n<p>How many words are in each sentence?<\/p>\n<pre>for sentence in text:\n  sentenceLength = len(sentence.split())\n  print(sentenceLength)\n\n3\n13\n<\/pre>\n<p>But I want to do it all at once the functional programming way, with maps.<\/p>\n<pre>list(map(len, map(split, text)))\n\nNameError: name 'split' is not defined\n<\/pre>\n<p>Why does that produce an error? Because &#8220;split&#8221; isn&#8217;t a function. It&#8217;s a method of strings: <code>str.split()<\/code>, not <code>split(str)<\/code>.<\/p>\n<p>So how do we use map with a class method?<\/p>\n<pre>from operator import methodcaller\nsplit = methodcaller(\"split\")\n<\/pre>\n<p>That means, &#8220;Create a function. I&#8217;ll pass in an object. Call its &#8216;split&#8217; method.&#8221;<\/p>\n<p>Now it works.<\/p>\n<pre>list(map(len, map(split, text)))\n[3, 13]\n<\/pre>\n<p>Of course, there are other ways. Don&#8217;t even need map.<\/p>\n<pre>[len(s.split()) for s in text]<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How do I use <code>map<\/code> with an object&#8217;s method?<br \/>\nWhy doesn&#8217;t <code>map(split, listOfStrings)<\/code> work?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[15,3],"tags":[],"class_list":["post-588","post","type-post","status-publish","format-standard","hentry","category-python","category-technical"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2IBF1-9u","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=588"}],"version-history":[{"count":6,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/588\/revisions"}],"predecessor-version":[{"id":594,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=\/wp\/v2\/posts\/588\/revisions\/594"}],"wp:attachment":[{"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thenoyes.com\/littlenoise\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}