<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Instead of setting instance attributes within __init__</title>
	<atom:link href="http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/</link>
	<description>Programming, gardening, economics, life in Cleveland Heights</description>
	<lastBuildDate>Sat, 04 Feb 2012 03:03:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: cheap condoms</title>
		<link>http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/comment-page-1/#comment-3466</link>
		<dc:creator>cheap condoms</dc:creator>
		<pubDate>Mon, 21 Sep 2009 15:25:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tplus1.com/?p=310#comment-3466</guid>
		<description>In my opinion, __init__ is for initializing stuff, so what&#039;s wrong with initializing instance attributes there?You can always break up the argument parsing bit from the default values bit with a comment.</description>
		<content:encoded><![CDATA[<p>In my opinion, __init__ is for initializing stuff, so what&#39;s wrong with initializing instance attributes there?You can always break up the argument parsing bit from the default values bit with a comment.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: cheap condoms</title>
		<link>http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/comment-page-1/#comment-2439</link>
		<dc:creator>cheap condoms</dc:creator>
		<pubDate>Mon, 21 Sep 2009 11:25:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tplus1.com/?p=310#comment-2439</guid>
		<description>In my opinion, __init__ is for initializing stuff, so what&#039;s wrong with initializing instance attributes there?You can always break up the argument parsing bit from the default values bit with a comment.</description>
		<content:encoded><![CDATA[<p>In my opinion, __init__ is for initializing stuff, so what&#39;s wrong with initializing instance attributes there?You can always break up the argument parsing bit from the default values bit with a comment.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Resumes</title>
		<link>http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/comment-page-1/#comment-2360</link>
		<dc:creator>Resumes</dc:creator>
		<pubDate>Thu, 10 Sep 2009 17:30:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tplus1.com/?p=310#comment-2360</guid>
		<description>The first actually works well and gets the job done - trusted and proven. As for your experiment, I can&#039;t wait to try it for myself and see how it betters the latter. Cheers for the effort Matt!</description>
		<content:encoded><![CDATA[<p>The first actually works well and gets the job done &#8211; trusted and proven. As for your experiment, I can&#39;t wait to try it for myself and see how it betters the latter. Cheers for the effort Matt!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bc</title>
		<link>http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/comment-page-1/#comment-1608</link>
		<dc:creator>bc</dc:creator>
		<pubDate>Thu, 26 Mar 2009 16:06:14 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tplus1.com/?p=310#comment-1608</guid>
		<description>Here&#039;s an indented version, in case it&#039;s not obvious how it should have read:&lt;br&gt;&lt;br&gt;class MyClass(HasTraits):&lt;br&gt;....a = Float(1.234) #default value declared statically&lt;br&gt;.&lt;br&gt;.&lt;br&gt;class AnotherClass(HasTraits):&lt;br&gt;....a = Float()&lt;br&gt;&lt;br&gt;....def _a_default(self): #dynamic init setup using naming convention&lt;br&gt;........return 1.234</description>
		<content:encoded><![CDATA[<p>Here&#39;s an indented version, in case it&#39;s not obvious how it should have read:</p>
<p>class MyClass(HasTraits):<br />&#8230;.a = Float(1.234) #default value declared statically<br />.<br />.<br />class AnotherClass(HasTraits):<br />&#8230;.a = Float()</p>
<p>&#8230;.def _a_default(self): #dynamic init setup using naming convention<br />&#8230;&#8230;..return 1.234</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bc</title>
		<link>http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/comment-page-1/#comment-1607</link>
		<dc:creator>bc</dc:creator>
		<pubDate>Thu, 26 Mar 2009 16:03:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tplus1.com/?p=310#comment-1607</guid>
		<description>You might be interested in Traits (&lt;a href=&quot;http://code.enthought.com/projects/traits/&quot; rel=&quot;nofollow&quot;&gt;http://code.enthought.com/projects/traits/&lt;/a&gt;). Traits-based classes use declarative (static) initialisation or dynamic (on demand) initialisation like so:&lt;br&gt;&lt;br&gt;from enthought.traits.api import HasTraits, Float&lt;br&gt;&lt;br&gt;class MyClass(HasTraits):&lt;br&gt;    a = Float(1.234) #default value declared statically&lt;br&gt;&lt;br&gt;class AnotherClass(HasTraits):&lt;br&gt;    a = Float()&lt;br&gt;&lt;br&gt;    def _a_default(self): #dynamic init setup using naming convention&lt;br&gt;        return 1.234&lt;br&gt;&lt;br&gt;for the later, the &#039;a&#039; attribute is calculated on demand.&lt;br&gt;&lt;br&gt;Traits also does runtime type checking, notification, delegation and GUI-generation. There are also Property traits which are cacheable and handle depenencies. There are predefined traits for all the standard types and you can subclass your own.</description>
		<content:encoded><![CDATA[<p>You might be interested in Traits (<a href="http://code.enthought.com/projects/traits/" rel="nofollow">http://code.enthought.com/projects/traits/</a>). Traits-based classes use declarative (static) initialisation or dynamic (on demand) initialisation like so:</p>
<p>from enthought.traits.api import HasTraits, Float</p>
<p>class MyClass(HasTraits):<br />    a = Float(1.234) #default value declared statically</p>
<p>class AnotherClass(HasTraits):<br />    a = Float()</p>
<p>    def _a_default(self): #dynamic init setup using naming convention<br />        return 1.234</p>
<p>for the later, the &#39;a&#39; attribute is calculated on demand.</p>
<p>Traits also does runtime type checking, notification, delegation and GUI-generation. There are also Property traits which are cacheable and handle depenencies. There are predefined traits for all the standard types and you can subclass your own.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: saluk</title>
		<link>http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/comment-page-1/#comment-1605</link>
		<dc:creator>saluk</dc:creator>
		<pubDate>Thu, 26 Mar 2009 08:31:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tplus1.com/?p=310#comment-1605</guid>
		<description>I have the opposite opinion - __init__ should not be doing anything OTHER than setting the default attributes.  When I type a = Object(), I want a to be valid.  Putting too much stuff inside of __init__ can make it harder to see why something is actually failing.  Sometimes though, I do delegate out to other functions from __init__ which do more work if necessary.&lt;br&gt;&lt;br&gt;The above just looks like a mess!</description>
		<content:encoded><![CDATA[<p>I have the opposite opinion &#8211; __init__ should not be doing anything OTHER than setting the default attributes.  When I type a = Object(), I want a to be valid.  Putting too much stuff inside of __init__ can make it harder to see why something is actually failing.  Sometimes though, I do delegate out to other functions from __init__ which do more work if necessary.</p>
<p>The above just looks like a mess!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Finney</title>
		<link>http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/comment-page-1/#comment-1604</link>
		<dc:creator>Ben Finney</dc:creator>
		<pubDate>Wed, 25 Mar 2009 23:34:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tplus1.com/?p=310#comment-1604</guid>
		<description>One problem is it&#039;s more verbose, and rather unusual. The reader isn&#039;t going to expect what you&#039;re doing, which is always a negative when deciding whether to be clever in code :-)&lt;br&gt;&lt;br&gt;Another problem is you&#039;ve defined only a read-only property: the decorator wraps the getter, but the property has no setter (or deleter or docstring).</description>
		<content:encoded><![CDATA[<p>One problem is it&#39;s more verbose, and rather unusual. The reader isn&#39;t going to expect what you&#39;re doing, which is always a negative when deciding whether to be clever in code <img src='http://blog.tplus1.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Another problem is you&#39;ve defined only a read-only property: the decorator wraps the getter, but the property has no setter (or deleter or docstring).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wyatt Baldwin</title>
		<link>http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/comment-page-1/#comment-1603</link>
		<dc:creator>Wyatt Baldwin</dc:creator>
		<pubDate>Wed, 25 Mar 2009 20:31:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tplus1.com/?p=310#comment-1603</guid>
		<description>&gt;&gt;&gt; class C(object):&lt;br&gt;............@property&lt;br&gt;............def a(self):&lt;br&gt;................try:&lt;br&gt;....................self._a&lt;br&gt;................except AttributeError:&lt;br&gt;....................# Do possibly expensive stuff here&lt;br&gt;....................self._a = &lt;result of possibly expensive stuff&gt;&lt;br&gt;................return self._a</description>
		<content:encoded><![CDATA[<p>&gt;&gt;&gt; class C(object):<br />&#8230;&#8230;&#8230;&#8230;@property<br />&#8230;&#8230;&#8230;&#8230;def a(self):<br />&#8230;&#8230;&#8230;&#8230;&#8230;.try:<br />&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..self._a<br />&#8230;&#8230;&#8230;&#8230;&#8230;.except AttributeError:<br />&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..# Do possibly expensive stuff here<br />&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;..self._a = &lt;result of possibly expensive stuff&gt;<br />&#8230;&#8230;&#8230;&#8230;&#8230;.return self._a</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wyatt Baldwin</title>
		<link>http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/comment-page-1/#comment-1602</link>
		<dc:creator>Wyatt Baldwin</dc:creator>
		<pubDate>Wed, 25 Mar 2009 20:25:02 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tplus1.com/?p=310#comment-1602</guid>
		<description>I use this technique frequently, though with a different style, and usually just for something that&#039;s expensive to calculate (and can be cached):&lt;br&gt;&lt;br&gt;&gt;&gt;&gt; class C(object):&lt;br&gt;... &lt;br&gt;...     @property&lt;br&gt;...     def a(self):&lt;br&gt;...         try:&lt;br&gt;...             self._a&lt;br&gt;...         except AttributeError:&lt;br&gt;...             # Do possibly expensive stuff here&lt;br&gt;...             self._a  = &lt;result of possibly expensive stuff&gt;&lt;br&gt;...         return self._a&lt;br&gt;&lt;br&gt;I think this has better performance than using hasattr. Also, I wonder if having a @property and instance variable with the same name would cause problems somewhere; that&#039;s why I use `_a`.&lt;br&gt;&lt;br&gt;Note: I didn&#039;t read other comments first. Sorry for any repetition.</description>
		<content:encoded><![CDATA[<p>I use this technique frequently, though with a different style, and usually just for something that&#39;s expensive to calculate (and can be cached):</p>
<p>&gt;&gt;&gt; class C(object):<br />&#8230; <br />&#8230;     @property<br />&#8230;     def a(self):<br />&#8230;         try:<br />&#8230;             self._a<br />&#8230;         except AttributeError:<br />&#8230;             # Do possibly expensive stuff here<br />&#8230;             self._a  = &lt;result of possibly expensive stuff&gt;<br />&#8230;         return self._a</p>
<p>I think this has better performance than using hasattr. Also, I wonder if having a @property and instance variable with the same name would cause problems somewhere; that&#39;s why I use `_a`.</p>
<p>Note: I didn&#39;t read other comments first. Sorry for any repetition.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sk</title>
		<link>http://blog.tplus1.com/index.php/2009/03/24/instead-of-setting-instance-attributes-within-__init__/comment-page-1/#comment-1601</link>
		<dc:creator>sk</dc:creator>
		<pubDate>Wed, 25 Mar 2009 20:18:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tplus1.com/?p=310#comment-1601</guid>
		<description>How about initializing instance variables first thing inside __init__. Then you can call descriptively named methods that each do their own &quot;interesting thing&quot;. Finally, one line above the class declaration can be a &lt;a href=&quot;http://jackdied.com/jackdied/decos_and_metas_uk.pdf&quot; rel=&quot;nofollow&quot;&gt;class decorator&lt;/a&gt; (PDF) that &quot;makes sure that instances of my class have a bunch of attributes&quot;.</description>
		<content:encoded><![CDATA[<p>How about initializing instance variables first thing inside __init__. Then you can call descriptively named methods that each do their own &#8220;interesting thing&#8221;. Finally, one line above the class declaration can be a <a href="http://jackdied.com/jackdied/decos_and_metas_uk.pdf" rel="nofollow">class decorator</a> (PDF) that &#8220;makes sure that instances of my class have a bunch of attributes&#8221;.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

