{"id":5135,"date":"2023-08-21T02:05:28","date_gmt":"2023-08-21T10:05:28","guid":{"rendered":"https:\/\/wonghoi.humgar.com\/blog\/?p=5135"},"modified":"2025-12-01T02:22:38","modified_gmt":"2025-12-01T10:22:38","slug":"python-cheat-sheet","status":"publish","type":"post","link":"https:\/\/wonghoi.humgar.com\/blog\/2023\/08\/21\/python-cheat-sheet\/","title":{"rendered":"Python Cheat Sheet"},"content":{"rendered":"\n<p><a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#property\">Built-in functions<\/a><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>breakpoint()<\/code>: Python&#8217;s version of MATLAB&#8217;s <code>keyboard()<\/code> command<\/li>\n\n\n\n<li><code>callable()<\/code>: Like MATLAB&#8217;s <code>isfunction()<\/code> but it really checks if there&#8217;s a <code>__call__<\/code> method<\/li>\n\n\n\n<li><code>getattr()<\/code>\/<code>hasattr()<\/code>: MATLAB&#8217;s <code>getfield()<\/code>\/<code>isfield()<\/code>. The 3rd parameter of <code>getattr()<\/code> is a shortcut to spit out a default if there&#8217;s no such field\/attribute, which MATLAB doesn&#8217;t have<\/li>\n\n\n\n<li><code>globals()<\/code>\/<code>locals()<\/code>: more convenient than MATLAB because the whole workspace (current variables) are accessed as a dictionary in Python by calling <code>locals()<\/code> and <code>globals()<\/code><\/li>\n\n\n\n<li><code>id()<\/code>: memory address of the item where the variable (reference) is pointing to. Think of it as <code>&amp;x<\/code> in C. MATLAB doesn&#8217;t do alias or pointers.<\/li>\n\n\n\n<li><code>isinstance()<\/code>: MATLAB&#8217;s <code>isa()<\/code><\/li>\n\n\n\n<li><code>next()<\/code>: Python favors not actually computing the values until needed so instead it offers a generator (forward iterable) function that spits out one value at each time you kick it with <code>next()<\/code> and you can&#8217;t go back. MATLAB does not do iterators.<\/li>\n\n\n\n<li><code>chr()<\/code>\/<code>ord()<\/code>: analogous to MATLAB&#8217;s <code>char()<\/code>\/<code>double()<\/code> cast for characters<\/li>\n\n\n\n<li>Python&#8217;s exponentiation is <code>**<\/code>, not <code>^<\/code> like most other languages (C does not have exponentiation symbol, and <code>^<\/code> was used for xor)<\/li>\n\n\n\n<li><code>print(..., flush=false)<\/code> allows a courtesy flush<\/li>\n\n\n\n<li><code>repr()<\/code>: MATLAB&#8217;s version of <code>disp()<\/code>, also overloadable standard interface<\/li>\n\n\n\n<li><code>slice()<\/code>: MATLAB&#8217;s equivalent of <code>colon()<\/code> special interface<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Context Manager<\/h2>\n\n\n\n<p><code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">@contextlib.contextmanager<\/code> decorators basically splits a <code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">set-try-yield-finally<\/code> boilerplate function into 3 parts: <code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">__enter__<\/code> (everything above yield), BODY (where the yield goes to) and <code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">__exit__<\/code> (everything below yield), since a <code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">with-as<\/code> statement is a rigidly defined <code data-enlighter-language=\"python\" class=\"EnlighterJSRAW\">try-finally<\/code> block, roughly like this:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">with EXPR as f:\n  BODY(using f)<\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">__enter__: f=EVAL(EXPR)\ntry:\n  # f isn't evaluated till yield  \n  yield f  # Goes to BODY\nfinally:\n  __exit__: cleanup(f)<\/pre>\n<\/div>\n<\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>&#8230; more to come as I have came across noteworthy cases.<\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_5135\" class=\"pvc_stats all  \" data-element-id=\"5135\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Built-in functions Context Manager @contextlib.contextmanager decorators basically splits a set-try-yield-finally boilerplate function into 3 parts: __enter__ (everything above yield), BODY (where the yield goes to) and __exit__ (everything below yield), since a with-as statement is a rigidly defined try-finally block, &hellip; <a href=\"https:\/\/wonghoi.humgar.com\/blog\/2023\/08\/21\/python-cheat-sheet\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_5135\" class=\"pvc_stats all  \" data-element-id=\"5135\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/wonghoi.humgar.com\/blog\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[30,25,10,29,34],"tags":[],"class_list":["post-5135","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-cpp","category-matlab","category-programming","category-python"],"_links":{"self":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/comments?post=5135"}],"version-history":[{"count":10,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5135\/revisions"}],"predecessor-version":[{"id":6724,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/posts\/5135\/revisions\/6724"}],"wp:attachment":[{"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/media?parent=5135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/categories?post=5135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wonghoi.humgar.com\/blog\/wp-json\/wp\/v2\/tags?post=5135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}