<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>t+1 - Latest Comments in Instead of setting instance attributes within __init__</title><link>http://tplus1.disqus.com/</link><description></description><atom:link href="https://tplus1.disqus.com/instead_of_setting_instance_attributes_within___init__/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Mon, 21 Sep 2009 06:25:59 -0000</lastBuildDate><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-17030345</link><description>&lt;p&gt;In my opinion, __init__ is for initializing stuff, so what's wrong with initializing instance attributes there?You can always break up the argument parsing bit from the default values bit with a comment.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">cheap condoms</dc:creator><pubDate>Mon, 21 Sep 2009 06:25:59 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7526284</link><description>&lt;p&gt;Here's an indented version, in case it's not obvious how it should have read:&lt;/p&gt;&lt;p&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;/p&gt;&lt;p&gt;....def _a_default(self): #dynamic init setup using naming convention&lt;br&gt;........return 1.234&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">bc</dc:creator><pubDate>Thu, 26 Mar 2009 11:06:14 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7526186</link><description>&lt;p&gt;You might be interested in Traits (&lt;a href="http://code.enthought.com/projects/traits/)" rel="nofollow noopener" target="_blank" title="http://code.enthought.com/projects/traits/)"&gt;http://code.enthought.com/p...&lt;/a&gt;. Traits-based classes use declarative (static) initialisation or dynamic (on demand) initialisation like so:&lt;/p&gt;&lt;p&gt;from enthought.traits.api import HasTraits, Float&lt;/p&gt;&lt;p&gt;class MyClass(HasTraits):&lt;br&gt;    a = Float(1.234) #default value declared statically&lt;/p&gt;&lt;p&gt;class AnotherClass(HasTraits):&lt;br&gt;    a = Float()&lt;/p&gt;&lt;p&gt;    def _a_default(self): #dynamic init setup using naming convention&lt;br&gt;        return 1.234&lt;/p&gt;&lt;p&gt;for the later, the 'a' attribute is calculated on demand.&lt;/p&gt;&lt;p&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.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">bc</dc:creator><pubDate>Thu, 26 Mar 2009 11:03:05 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7518503</link><description>&lt;p&gt;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;/p&gt;&lt;p&gt;The above just looks like a mess!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">saluk</dc:creator><pubDate>Thu, 26 Mar 2009 03:31:16 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7505952</link><description>&lt;p&gt;One problem is it's more verbose, and rather unusual. The reader isn't going to expect what you're doing, which is always a negative when deciding whether to be clever in code :-)&lt;/p&gt;&lt;p&gt;Another problem is you've defined only a read-only property: the decorator wraps the getter, but the property has no setter (or deleter or docstring).&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ben Finney</dc:creator><pubDate>Wed, 25 Mar 2009 18:34:44 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7501296</link><description>&lt;p&gt;&amp;gt;&amp;gt;&amp;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 = &amp;lt;result of="" possibly="" expensive="" stuff=""&amp;gt;&lt;br&gt;................return self._a&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Wyatt Baldwin</dc:creator><pubDate>Wed, 25 Mar 2009 15:31:52 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7501115</link><description>&lt;p&gt;I use this technique frequently, though with a different style, and usually just for something that's expensive to calculate (and can be cached):&lt;/p&gt;&lt;p&gt;&amp;gt;&amp;gt;&amp;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  = &amp;lt;result of="" possibly="" expensive="" stuff=""&amp;gt;&lt;br&gt;...         return self._a&lt;/p&gt;&lt;p&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's why I use `_a`.&lt;/p&gt;&lt;p&gt;Note: I didn't read other comments first. Sorry for any repetition.&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Wyatt Baldwin</dc:creator><pubDate>Wed, 25 Mar 2009 15:25:02 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7500954</link><description>&lt;p&gt;How about initializing instance variables first thing inside __init__. Then you can call descriptively named methods that each do their own "interesting thing". Finally, one line above the class declaration can be a &lt;a href="http://jackdied.com/jackdied/decos_and_metas_uk.pdf" rel="nofollow noopener" target="_blank" title="http://jackdied.com/jackdied/decos_and_metas_uk.pdf"&gt;class decorator&lt;/a&gt; (PDF) that "makes sure that instances of my class have a bunch of attributes". &lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">sk</dc:creator><pubDate>Wed, 25 Mar 2009 15:18:24 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7493835</link><description>&lt;p&gt;Seems like a lot of mechanism.  If the goal is to reduce the size of __init__ why not use a function like &lt;br&gt;def __init_vars(self):&lt;br&gt;    self.a=1&lt;br&gt;    self.b=2&lt;br&gt;and call that from the __init__ function.&lt;/p&gt;&lt;p&gt;In both cases I would worry about tools not understanding what is going on.  Pulling the variable initialization out of the __init__ function will fool programs like pylint and doxygen.&lt;/p&gt;&lt;p&gt;I would probably just create sections in my __init__ like:&lt;/p&gt;&lt;p&gt;def __init(self):&lt;br&gt;    #&lt;br&gt;    # Variable &lt;br&gt;    #&lt;br&gt;    self.a=1&lt;br&gt;    self.b=2&lt;/p&gt;&lt;p&gt;    #&lt;br&gt;    # Object setup&lt;br&gt;    #&lt;br&gt;    do interesting stuff here&lt;/p&gt;&lt;p&gt;But then again I do a lot of support programming and I like boring simple programs.   They make it easier for me to understand.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Paul Hildebrandt</dc:creator><pubDate>Wed, 25 Mar 2009 11:07:04 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7492748</link><description>&lt;p&gt;Guess you should really use the __init__ methods to initialize your attributes. If you really don't want to do that, you can use some metaprogramming though:&lt;/p&gt;&lt;p&gt;class Test:&lt;br&gt;    def __getattr__(self,attr):&lt;br&gt;        self.__dict__[attr] = 1&lt;br&gt;        return self.__dict__[attr]&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Rodrigo</dc:creator><pubDate>Wed, 25 Mar 2009 10:22:09 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7492718</link><description>&lt;p&gt;why not something like:&lt;/p&gt;&lt;p&gt;class C(object):&lt;br&gt;    a = 1&lt;br&gt;    b = 2&lt;br&gt;    def __init__(self):&lt;br&gt;        # Init code goes here&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Chuck</dc:creator><pubDate>Wed, 25 Mar 2009 10:21:38 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7491595</link><description>&lt;p&gt;Oops, comments erased my spaces. Hopefully more readable version:&lt;/p&gt;&lt;p&gt;&amp;gt;&amp;gt;&amp;gt;class D(object):&lt;br&gt;...    __slots__ = ('a', 'b') &lt;br&gt;...    def __init__(self, attr_defaults={'a': 1,'b': 2}):&lt;br&gt;...        [setattr(self, key, value) for key, value in attr_defaults.items()]&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">James</dc:creator><pubDate>Wed, 25 Mar 2009 09:26:00 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7491558</link><description>&lt;p&gt;If you're really worried about length, I guess you could do something like this:&lt;/p&gt;&lt;p&gt;class D(object):&lt;br&gt;    __slots__ = ('a', 'b')&lt;/p&gt;&lt;p&gt;    def __init__(self, attr_defaults={'a': 1,'b': 2}):&lt;br&gt;        [setattr(self, key, value) for key, value in attr_defaults.items()]&lt;/p&gt;&lt;p&gt;...seems to me to retain readability, although it's not the usual way of doing things. &lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">James</dc:creator><pubDate>Wed, 25 Mar 2009 09:24:08 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7491117</link><description>&lt;p&gt;The best reason to do it in __init__ is because everyone reading your code expects it to be done in __init__.  In a few cases I've done it in a reset() method that is the first thing called in __init__.&lt;/p&gt;&lt;p&gt;Also, are you sure you're not getting a loop in that 'a' method?  The first time you actually access the property it should loop and eventually bail with an error. [I just tried it - it quits after 999 recursive calls and raises an AttributeError]&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Jack Diederich</dc:creator><pubDate>Wed, 25 Mar 2009 08:58:34 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7489117</link><description>&lt;p&gt;Maybe you shouldn't try to do real work in __init__()?&lt;/p&gt;&lt;p&gt;&lt;a href="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/" rel="nofollow noopener" target="_blank" title="http://misko.hevery.com/code-reviewers-guide/flaw-constructor-does-real-work/"&gt;http://misko.hevery.com/cod...&lt;/a&gt;&lt;/p&gt;&lt;p&gt;I'm not saying this is always the best solution, but do find that when I think about this while wanting to do real work in __init__() often I find an alternative way of doing things that's rather elegant.  And the line of what "real" work is can be quite fine too sometimes.&lt;/p&gt;&lt;p&gt;Oh, I also agree with the comments about properties taking up so many more lines.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Floris Bruynooghe</dc:creator><pubDate>Wed, 25 Mar 2009 05:46:53 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7488896</link><description>&lt;p&gt;Excellent question!  Because we don't want Python to be like Perl :)  Recently I stumbled on this quote from Damian Conway in Perl Best Practices,&lt;br&gt;"Perl's approach to object oriented is almost excessively Perlish: there are far too many ways to do it....&lt;br&gt;There are just so many possible combinations of implementation, structure, and semantics that it's quite rare&lt;br&gt;to find two unrelated class hierarchies that use precisely the same style of Perl OO."&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Noah Gift</dc:creator><pubDate>Wed, 25 Mar 2009 05:05:48 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7488599</link><description>&lt;p&gt;Have you tried to run your code, it does not, and can never work.&lt;/p&gt;&lt;p&gt;&amp;gt;&amp;gt;&amp;gt; class D(object):&lt;br&gt;...     @property&lt;br&gt;...     def a(self):&lt;br&gt;...         if not hasattr(self, 'a'):&lt;br&gt;...             self.a  =1  &lt;br&gt;... &lt;br&gt;&amp;gt;&amp;gt;&amp;gt; &lt;br&gt;&amp;gt;&amp;gt;&amp;gt; d = D() &lt;br&gt;&amp;gt;&amp;gt;&amp;gt; print d.a &lt;br&gt;Traceback (most recent call last):&lt;br&gt;  File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;&lt;br&gt;  File "&amp;lt;stdin&amp;gt;", line 5, in a&lt;br&gt;AttributeError: can't set attribute&lt;br&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Simon Wittber</dc:creator><pubDate>Wed, 25 Mar 2009 04:15:32 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7488400</link><description>&lt;p&gt;Hi,&lt;br&gt;Every property takes 5 lines instead of 1? And you were worried about length???&lt;/p&gt;&lt;p&gt;You could call a separate method from init to do the setting.&lt;/p&gt;&lt;p&gt;- Paddy.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Paddy3118</dc:creator><pubDate>Wed, 25 Mar 2009 03:44:16 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7488196</link><description>&lt;p&gt;What ben said. This way I'd have to look through the much more verbose code for every property to get a view of the default values, rather than having them all in one place in a more concise form.&lt;/p&gt;&lt;p&gt;You can always break up the argument parsing bit from the default values bit with a comment.&lt;/p&gt;&lt;p&gt;Simon Hibbs&lt;br&gt;&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Simon Hibbs</dc:creator><pubDate>Wed, 25 Mar 2009 03:12:56 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7487262</link><description>&lt;p&gt;From the example it looks like you just want some class attributes holding those default values...&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Richard Jones</dc:creator><pubDate>Wed, 25 Mar 2009 01:20:04 -0000</pubDate></item><item><title>Re: Instead of setting instance attributes within __init__</title><link>http://blog.tplus1.com/blog/2009/03/24/instead-of-setting-instance-attributes-within-__init__/#comment-7485533</link><description>&lt;p&gt;I can't speak for any technical pitfalls you may have, but I do have a concern with maintainability..&lt;/p&gt;&lt;p&gt;In my opinion, __init__ is for initializing stuff, so what's wrong with initializing instance attributes there?  Seems to me that your way abstracts that in the sense that it takes what would I would normally look for in __init__ and put it somewhere else..  Just a thought that I'm sure you've had already :)&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Benjamin W. Smith</dc:creator><pubDate>Tue, 24 Mar 2009 23:29:14 -0000</pubDate></item></channel></rss>