{"id":6494,"date":"2020-01-25T21:42:06","date_gmt":"2020-01-25T20:42:06","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=6494"},"modified":"2020-01-25T21:42:06","modified_gmt":"2020-01-25T20:42:06","slug":"a-quick-dive-into-pythons-__slots__-noteworthy-the-journal-blog","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2020\/01\/25\/a-quick-dive-into-pythons-__slots__-noteworthy-the-journal-blog\/","title":{"rendered":"A quick dive into Python\u2019s \u201c__slots__\u201d &#8211; Noteworthy &#8211; The Journal Blog"},"content":{"rendered":"<p><em><a href=\"https:\/\/blog.usejournal.com\/a-quick-dive-into-pythons-slots-72cdc2d334e\">A quick dive into Python\u2019s \u201c<strong>slots<\/strong>\u201d &#8211; Noteworthy &#8211; The Journal Blog<\/a><\/em><\/p>\n<p>Or how to try mimik a strongly typed language in a weakly typed one or something like this&#8230;.<\/p>\n<p><!--nextpage--><\/p>\n<blockquote>\n<div id=\"4c7a\" class=\"fj fk cn ar fl b fm fn fo fp fq fr fs ft fu fv fw\">\n<h1 class=\"fl b fm fx fo fy fq fz fs ga fu gb cn\">A quick dive into Python\u2019s \u201c__slots__\u201d<\/h1>\n<\/div>\n<div class=\"gc\">\n<div class=\"n gd ge gf gg\">\n<div class=\"o n\">\n<div><a href=\"https:\/\/blog.usejournal.com\/@stephenjayakar?source=post_page-----72cdc2d334e----------------------\" rel=\"noopener\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"r dg gh gi\" src=\"https:\/\/i0.wp.com\/miro.medium.com\/fit\/c\/96\/96\/2%2AvFNVadKjiDYAViJUxGzsTg.jpeg?resize=48%2C48&#038;ssl=1\" alt=\"Stephen Jayakar\" width=\"48\" height=\"48\" \/><\/a><\/div>\n<div class=\"gj ak r\">\n<div class=\"n\">\n<div>\n<div class=\"gk n o gl\"><span class=\"aq ef bj as bd gm bc gn go gp cn\"><a class=\"bo bp bq br bs bt bu bv bw bx gq ca cb cc cd\" href=\"https:\/\/blog.usejournal.com\/@stephenjayakar?source=post_page-----72cdc2d334e----------------------\" rel=\"noopener\">Stephen Jayakar<\/a><\/span><\/p>\n<div class=\"gr r ap h\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"o n\">\n<div class=\"gj ak r\">\n<div><a class=\"bo bp bq br bs bt bu bv bw bx gq ca cb cc cd\" href=\"https:\/\/blog.usejournal.com\/a-quick-dive-into-pythons-slots-72cdc2d334e?source=post_page-----72cdc2d334e----------------------\" rel=\"noopener\">Nov 10, 2018<\/a> \u00b7 6 min read<\/div>\n<\/div>\n<\/div>\n<p id=\"049b\" class=\"il im cn ar in b io ip iq ir is it iu iv iw ix iy fd\" data-selectable-paragraph=\"\">or my first Medium article, we\u2019re going to go into a quick and easy way to speed up your Python code (and pass those pesky HackerRank tests where you\u2019re just a bit short on time!), as well as some of the technical implementation details for the curious.<\/p>\n<p id=\"4ae0\" class=\"il im cn ar in b io ip iq ir is it iu iv iw ix iy fd\" data-selectable-paragraph=\"\"><code class=\"\" data-line=\"\">__slots__<\/code> is an attribute you can add to a Python class when defining it. You define slots with the possible attributes that an instance of an object can possess. Here\u2019s how you use <code class=\"\" data-line=\"\">__slots__<\/code>:<\/p>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-__slots__-example-1-py-LC1\" class=\"blob-code blob-code-inner js-file-line\"><span class=\"pl-k\">class<\/span> <span class=\"pl-v\">WithSlots<\/span>:<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-__slots__-example-1-py-LC2\" class=\"blob-code blob-code-inner js-file-line\"><span class=\"pl-s1\">__slots__<\/span> <span class=\"pl-c1\">=<\/span> (<span class=\"pl-s\">&#8216;x&#8217;<\/span>, <span class=\"pl-s\">&#8216;y&#8217;<\/span>)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-__slots__-example-1-py-LC3\" class=\"blob-code blob-code-inner js-file-line\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table class=\"highlight tab-size js-file-line-container\" data-tab-size=\"8\">\n<tbody>\n<tr>\n<td id=\"file-__slots__-example-1-py-LC4\" class=\"blob-code blob-code-inner js-file-line\"><span class=\"pl-k\">def<\/span> <span class=\"pl-en\">__init__<\/span>(<span class=\"pl-s1\">self<\/span>, <span class=\"pl-s1\">x<\/span>, <span class=\"pl-s1\">y<\/span>):<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p data-selectable-paragraph=\"\"><span class=\"pl-s1\">self<\/span>.<span class=\"pl-s1\">x<\/span>, <span class=\"pl-s1\">self<\/span>.<span class=\"pl-s1\">y<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s1\">x<\/span>, <span class=\"pl-s1\">y<\/span><\/p>\n<p id=\"3763\" class=\"il im cn ar in b io ip iq ir is it iu iv iw ix iy fd\" data-selectable-paragraph=\"\">For instances of this class, you can use <code class=\"\" data-line=\"\">self.x<\/code> and <code class=\"\" data-line=\"\">self.y<\/code> in the same ways as a normal class instance. However, one key difference between this and instancing from a normal class is that you <em class=\"je\">cannot add or remove<\/em> attributes from this class\u2019 instances. Say the instance was called <code class=\"\" data-line=\"\">w<\/code>: you couldn\u2019t write <code class=\"\" data-line=\"\">w.z = 2<\/code> without causing an error.<\/p>\n<p id=\"7238\" class=\"il im cn ar in b io ip iq ir is it iu iv iw ix iy fd\" data-selectable-paragraph=\"\">The biggest higher-level reasons to use <code class=\"\" data-line=\"\">__slots__<\/code> are 1) <strong class=\"in jf\">faster attribute getting and setting<\/strong> due to data structure optimization and 2) <strong class=\"in jf\">reduced memory usage<\/strong> for class instances. Some reasons you wouldn\u2019t want to use it is if your class has attributes that change during run-time (dynamic attributes) or if there\u2019s a complicated object inheritance tree.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">A quick dive into Python\u2019s \u201cslots\u201d &#8211; Noteworthy &#8211; The Journal Blog Or how to try mimik a strongly typed language in a weakly typed one or something like this&#8230;.<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2020\/01\/25\/a-quick-dive-into-pythons-__slots__-noteworthy-the-journal-blog\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_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,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","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}},"categories":[72,113],"tags":[],"class_list":["post-6494","post","type-post","status-publish","format-standard","hentry","category-documentations","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-1GK","jetpack-related-posts":[{"id":9039,"url":"https:\/\/monodes.com\/predaelli\/2022\/01\/01\/deciphering-glyph-the-one-python-library-everyone-needs\/","url_meta":{"origin":6494,"position":0},"title":"Deciphering Glyph :: The One Python Library Everyone Needs","author":"Paolo Redaelli","date":"2022-01-01","format":"link","excerpt":"Use attrs. Use it. Use it for everything. From: Deciphering Glyph :: The One Python Library Everyone Needs Looks a little like magic. But that's what happen with loosely typed languages, \"interpreted\" languages.","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/monodes.com\/predaelli\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11883,"url":"https:\/\/monodes.com\/predaelli\/2024\/08\/28\/python-shared-memory-in-multiprocessing\/","url_meta":{"origin":6494,"position":1},"title":"Python Shared Memory in Multiprocessing","author":"Paolo Redaelli","date":"2024-08-28","format":false,"excerpt":"Python Shared Memory in Multiprocessing np_array's size=220.0MB With SharedMemory: ... Current memory usage 0.11283MB; Peak: 0.156706MB Time elapsed: 0.99s No SharedMemory: ... Current memory usage 0.026587MB; Peak: 467.558995MB Time elapsed: 5.48s I think I shall go for shared memory! My Amiga formation years requires it! Python Shared Memory in Multiprocessing\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/monodes.com\/predaelli\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":8964,"url":"https:\/\/monodes.com\/predaelli\/2021\/12\/06\/rusteiffel\/","url_meta":{"origin":6494,"position":2},"title":"RustEiffel?","author":"Paolo Redaelli","date":"2021-12-06","format":false,"excerpt":"Python 4.0 will never arrive\ud83e\udd1a\ud83d\ude14. Said by Python\u2019s creator And the reason is mainly Rust. Guido van Rossum (the Python programming language\u2019s inventor) stated in an interview a few days ago that it would be difficult for Python 4.0 to see the light of day since the programming language is\u2026","rel":"","context":"In &quot;Liberty Eiffel&quot;","block_context":{"text":"Liberty Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/liberty-eiffel\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2021\/12\/1Zw72DLdFFS_SoumO0Ho_Ew.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2021\/12\/1Zw72DLdFFS_SoumO0Ho_Ew.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2021\/12\/1Zw72DLdFFS_SoumO0Ho_Ew.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2021\/12\/1Zw72DLdFFS_SoumO0Ho_Ew.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":14807,"url":"https:\/\/monodes.com\/predaelli\/2026\/01\/23\/the-xonsh-shell-python-powered-shell\/","url_meta":{"origin":6494,"position":3},"title":"The Xonsh Shell \u2014 Python-powered shell","author":"Paolo Redaelli","date":"2026-01-23","format":false,"excerpt":"The Xonsh Shell \u2014 Python-powered shell. Python shell. Python in the shell. Shell in Python. Shell and Python. Python and shell.Xonsh (sounds like \"consh\") is a modern, full-featured and cross-platform python shell. The language is a superset of Python 3.6+ with additional shell primitives that you are used to from\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/monodes.com\/predaelli\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2026\/01\/xonsh.webp?fit=257%2C399&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":13563,"url":"https:\/\/monodes.com\/predaelli\/2025\/06\/15\/what-can-we-learn\/","url_meta":{"origin":6494,"position":4},"title":"What can we learn?","author":"Paolo Redaelli","date":"2025-06-15","format":false,"excerpt":"I just read \"Python Creator Guido van Rossum Asks: Is 'Worse is Better' Still True for Programming Languages?\" and I wonder what can we Eiffellers learn from it. Python Creator Guido van Rossum Asks: Is 'Worse is Better' Still True for Programming Languages? In 1989 a computer scientist argued that\u2026","rel":"","context":"In &quot;Liberty Eiffel&quot;","block_context":{"text":"Liberty Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/liberty-eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3299,"url":"https:\/\/monodes.com\/predaelli\/2017\/08\/25\/micropython-python-for-microcontrollers\/","url_meta":{"origin":6494,"position":5},"title":"MicroPython &#8211; Python for microcontrollers","author":"Paolo Redaelli","date":"2017-08-25","format":false,"excerpt":"MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimised to run on microcontrollers and in constrained environments. Sorgente: MicroPython - Python for microcontrollers Well, pretty neat, as they deliver it in 256k of\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/monodes.com\/predaelli\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/6494","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=6494"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/6494\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=6494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=6494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=6494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}