{"id":17146,"date":"2022-10-23T07:00:01","date_gmt":"2022-10-23T01:30:01","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/"},"modified":"2022-10-23T07:00:01","modified_gmt":"2022-10-23T01:30:01","slug":"solved-explanation-of-c-implementation-pythons-len-function-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/","title":{"rendered":"[Solved] explanation of C implementation python&#8217;s len function [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-53988387\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"53988387\" data-parentid=\"53988132\" data-score=\"7\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>There is nothing special with <em>this<\/em> function. Usually the functions written in C, especially those that do not call Python code, are much faster than ones written in Python.<\/p>\n<p>I am specifically taking the stance here that a reader <em>knows<\/em> how C works, otherwise the explanation would rather be a book.<\/p>\n<p>The <code>builtin_len<\/code> is the one that is called when <code>len(foo)<\/code> is executed in Python code. The <code>PyObject *obj<\/code> argument to the function references the object given as the argument (<code>foo<\/code>), and <code>PyObject *self<\/code> will contain a reference to the containing module of <code>builtin_len<\/code>.<\/p>\n<p>Each container in Python must have a length between 0 and the maximum value allowed by <code>Py_ssize_t<\/code>. <code>PyObject_Size(obj);<\/code> is a function\/macro that gets the size of the given object through its <code>obj-&gt;ob_type-&gt;tp_as_sequence-&gt;sq_length<\/code> or <code>obj-&gt;ob_type-&gt;tp_as_mapping-&gt;mp_length<\/code>. Upon error an exception is set as raised for the current thread and a number &lt; 0 (-1) is returned.<\/p>\n<p>The <code>return NULL;<\/code> signifies the caller that an exception has occurred and it must act accordingly &#8211; if it is a function call instruction in Python bytecode, it will cause an exception to be raised; if it is C code, then it will behave in a manner similar to this function &#8211; returning NULL or invalid value if an exception occurred; or it can clear the exception or replace it with another one.<\/p>\n<p>Otherwise <strong>if greater than or equal to 0<\/strong>, the <code>Py_ssize_t res<\/code> which is of a C integer type, is converted to a Python <em><code>int<\/code><\/em> object, by either returning an existing <code>int<\/code> object or constructing a new one. The Python <code>int<\/code> object is called <code>PyLong<\/code> in CPython 3 for historical reasons. <code>PyLong_FromSsize_t()<\/code> is one of many functions &#8211; this one is able to convert any value of type <code>Py_ssize_t<\/code> to a Python <code>int<\/code> with the same value. The reference to this object, like all other objects, are kept as pointers to the (semi-opaque) <code>PyObject<\/code> structure, and this is returned.<\/p>\n<p>The <code>assert(PyErr_Occurred());<\/code> is an assertion that is in effect in debug builds of Python only. It asserts that upon getting a negative number from <code>PyObject_Size<\/code>, signifying an exception being thrown, the exception has also been properly set; if not present, it will abort the entire CPython process outright. It isn&#8217;t in effect in release builds of Python because &#8220;asserts never fail&#8221;.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved explanation of C implementation python&#8217;s len function [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] There is nothing special with this function. Usually the functions written in C, especially those that do not call Python code, are much faster than ones written in Python. I am specifically taking the stance here that a reader knows how C works, otherwise the explanation would rather be a book. The builtin_len is &#8230; <a title=\"[Solved] explanation of C implementation python&#8217;s len function [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/\" aria-label=\"More on [Solved] explanation of C implementation python&#8217;s len function [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,4282,349,1405],"class_list":["post-17146","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-cpython","tag-python","tag-python-internals"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] explanation of C implementation python&#039;s len function [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] explanation of C implementation python&#039;s len function [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] There is nothing special with this function. Usually the functions written in C, especially those that do not call Python code, are much faster than ones written in Python. I am specifically taking the stance here that a reader knows how C works, otherwise the explanation would rather be a book. The builtin_len is ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-23T01:30:01+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] explanation of C implementation python&#8217;s len function [closed]\",\"datePublished\":\"2022-10-23T01:30:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/\"},\"wordCount\":374,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"cpython\",\"python\",\"python-internals\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/\",\"name\":\"[Solved] explanation of C implementation python's len function [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-23T01:30:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] explanation of C implementation python&#8217;s len function [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] explanation of C implementation python's len function [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] explanation of C implementation python's len function [closed] - JassWeb","og_description":"[ad_1] There is nothing special with this function. Usually the functions written in C, especially those that do not call Python code, are much faster than ones written in Python. I am specifically taking the stance here that a reader knows how C works, otherwise the explanation would rather be a book. The builtin_len is ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/","og_site_name":"JassWeb","article_published_time":"2022-10-23T01:30:01+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] explanation of C implementation python&#8217;s len function [closed]","datePublished":"2022-10-23T01:30:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/"},"wordCount":374,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","cpython","python","python-internals"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/","name":"[Solved] explanation of C implementation python's len function [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-23T01:30:01+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-explanation-of-c-implementation-pythons-len-function-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] explanation of C implementation python&#8217;s len function [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=17146"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17146\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=17146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=17146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=17146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}