{"id":510,"date":"2007-04-25T08:02:50","date_gmt":"2007-04-25T15:02:50","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/2007\/04\/25\/fuck-the-python-borg-i-like-singleton-better\/"},"modified":"2007-04-25T08:02:50","modified_gmt":"2007-04-25T15:02:50","slug":"fuck-the-python-borg-i-like-singleton-better","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/fuck-the-python-borg-i-like-singleton-better\/","title":{"rendered":"Fuck the Python Borg, I like Singleton Better"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/farm1.static.flickr.com\/216\/467754686_701bee8596.jpg?w=640\" style=\"margin:3px 3px\" alt=\"I'm looking for a sponsor, H\u00c3\u00a4agen-Dazs wants a geek??\"><\/p>\n<p>I&#8217;ve read in parts of the web (and on the Martinelli&#8217;s Python Cookbok) that it&#8217;s better to do the Borg pattern over singletons, they say something alongs the lines of:<\/p>\n<p><strong>&#8220;who cares about identity, care about shared state&#8221;<\/strong><\/p>\n<p>Coming from the Java world, I just can&#8217;t understand that, why waste memory and cpu to address objects that share a state when you can have a single object in memory.<\/p>\n<p>If you&#8217;re looking on how to implement singleton in a simple manner with Python, do the following my friend:<\/p>\n<pre style=\"width:400px; border:none; font-weight:bolder\">\nclass MyClass:\n  __INSTANCE__ = None\n\n  def __init__(self):\n    #do whatever you need on your constructor\n    pass\n\n  @staticmethod\n  def getInstance():\n    if MyClass.__INSTANCE__ is None:\n      MyClass.__INSTANCE__ = MyClass()\n    return MyClass.__INSTANCE__\n\n#Then use it.\ntheOne = MyClass.getInstance()\n<\/pre>\n<p>Done deal, now start trolling on why this code sucks so we can fix it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve read in parts of the web (and on the Martinelli&#8217;s Python Cookbok) that it&#8217;s better to do the Borg pattern over singletons, they say something alongs the lines of: &#8220;who cares about identity, care about shared state&#8221; Coming from the Java world, I just can&#8217;t understand that, why waste memory and cpu to address [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","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_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"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,30,43,65],"tags":[],"class_list":["post-510","post","type-post","status-publish","format-standard","hentry","category-code","category-geeklife","category-linux","category-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-8e","jetpack-related-posts":[{"id":557,"url":"https:\/\/www.gubatron.com\/blog\/python-reference-binary-operators\/","url_meta":{"origin":510,"position":0},"title":"Python Reference: Binary Operators","author":"gubatron","date":"July 28, 2007","format":false,"excerpt":"Python binary operators are pretty much the same as in any other language, however I notice most programmers tend to waste a lot of memory by creating lots and lots of properties say in DB tables, or Objects and using the wrong datatypes. I think its elegant to use the\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":764,"url":"https:\/\/www.gubatron.com\/blog\/python-ip2num-num2ip-store-an-ip-string-as-a-4-byte-int\/","url_meta":{"origin":510,"position":1},"title":"[Python] ip2num \/ num2ip &#8211; Store an IP string as a 4 byte int.","author":"gubatron","date":"March 27, 2008","format":false,"excerpt":"This is probably everywhere, maybe python also comes with it, but I wanted to have my own implementation, and I'll leave it here for future reference. Basically, sometimes you don't want to store IPs in Strings cause they take too much space, instead you want to be a good programmer\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1114,"url":"https:\/\/www.gubatron.com\/blog\/using-a-linear-array-as-a-bidimensional-matrix\/","url_meta":{"origin":510,"position":2},"title":"Using a linear array as a bidimensional matrix","author":"gubatron","date":"January 27, 2009","format":false,"excerpt":"Often times I find the need to use a list or linear array as if it was a table. Everytime I need to do so, I always end up coding functions to convert a (x,y) coordinate to the real index n in the array. Let me illustrate, with an example.\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":446,"url":"https:\/\/www.gubatron.com\/blog\/como-es-que-convierto-de-decimal-a-binario\/","url_meta":{"origin":510,"position":3},"title":"Como es que convierto de decimal a binario?","author":"gubatron","date":"January 22, 2007","format":false,"excerpt":"Recuerdo que esto fue uno de los primeros ejercicios de programacion que me pusieron a hacer en Haskell, convertir de decimal a binario. Aqui una simple implementacion propia en python mientras estaba practicando pal google code jam (que la hice por gusto pq python tiene modulos para convertir de cualquier\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1015,"url":"https:\/\/www.gubatron.com\/blog\/python-script-to-update-wordpress-in-one-step\/","url_meta":{"origin":510,"position":4},"title":"Python Script to Update WordPress in One Step","author":"gubatron","date":"November 26, 2008","format":false,"excerpt":"During the past week, I think I had to update all my wordpress instances twice, and it's become really annoying doing this manually. I've written a python script which I'll share with you. How I keep my wordpress updated by hand I tend to keep my wp-content folder outside of\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":541,"url":"https:\/\/www.gubatron.com\/blog\/note-about-signals-and-pyqt4\/","url_meta":{"origin":510,"position":5},"title":"Note about Signals and PyQt4.","author":"gubatron","date":"July 9, 2007","format":false,"excerpt":"I keep making the mistake of sending PyQt_PyObjects instead of sending actual Qt4 objects on signals that are defined by Qt like that. Bottom line: If a signal has been defined by Qt, to send Qt objects, just copy and paste it, do not try to override it by exchanging\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/510","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=510"}],"version-history":[{"count":0,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/510\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=510"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=510"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=510"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}