<br>Just initialize everything in the constructor, unless you have <i>really </i>good reason not to do that. <br><div class="gmail_quote">On Sat, Mar 20, 2010 at 6:54 PM, Chris Rebert <span dir="ltr"><<a href="mailto:clp2@rebertia.com">clp2@rebertia.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On Sat, Mar 20, 2010 at 3:15 PM, kj <no.email@please.post> wrote:<br>
> I need to create a class solely for the purpose of encapsulating<br>
> a large number of disparate data items.  At the moment I have no<br>
> plans for any methods for this class other than the bazillion<br>
> accessors required to access these various instance variables.<br>
> (In case it matters, this class is meant to be a private helper<br>
> class internal to a module, and it won't be subclassed.)<br>
<br>
</div>If it's just a completely dumb struct-like class, you might consider<br>
something like:<br>
<a href="http://docs.python.org/library/collections.html#collections.namedtuple" target="_blank">http://docs.python.org/library/collections.html#collections.namedtuple</a><br>
<div class="im"><br>
> What is "best practice" for implementing this sort of class<br>
> *succinctly* (i.e. without a lot of repetitive accessor code)?<br>
<br>
</div>Is there any good reason you can't just use straight instance<br>
variables? Python ain't Java; vanilla, boilerplate accessor methods<br>
should almost always be avoided.<br>
<div class="im"><br>
> Also, one more question concerning syntax.  Suppose that i represents<br>
> an instance of this class.  Is it possible to define the class to<br>
> support this syntax<br>
><br>
>  val = i.field<br>
>  i.field += 6<br>
><br>
> ...rather than this one<br>
><br>
>  val = i.get_field()<br>
>  i.set_field(i.get_field() + 6)<br>
><br>
> ?<br>
<br>
</div>Yes, using the magic of the property() function:<br>
<a href="http://docs.python.org/library/functions.html#property" target="_blank">http://docs.python.org/library/functions.html#property</a><br>
<br>
Cheers,<br>
Chris<br>
<font color="#888888">--<br>
<a href="http://blog.rebertia.com" target="_blank">http://blog.rebertia.com</a><br>
</font><div><div></div><div class="h5">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>