<br><br><div class="gmail_quote">On Thu, May 22, 2008 at 11:47 AM, Leif Walsh <<a href="mailto:adlaiff6@gmail.com">adlaiff6@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">On Thu, 22 May 2008, Bruce Leban wrote:<br>
> I think it can better addressed by implementing NamedList and NamedDict:<br>
<br>
</div>Oh no...<br>
<div class="Ih2E3d"><br>
> NamedList(typename, fieldnames[, optionalfields[, verbose]])<br>
><br>
> Returns a new list subclass named typename. The new subclass is used to<br>
> create list-like objects that have fields accessible by attribute lookup as<br>
> well as supporting other list operations. Instances of a NamedList may be<br>
> created using a mixture of positional and keyword arguments. If<br>
> optionalfields is not true, then the NamedList must always contain at least<br>
> as many elements as the number of fields. If the NamedDict contains fewer<br>
> elements than the number of fields, missing fields return None when accessed<br>
> by attribute (a.third) and raise IndexError when accessed by index (a[3]).<br>
<br>
</div>Is this just a variable-length version of NamedTuple?  It doesn't seem<br>
to offer very much over NamedTuple to me, but I'm open to convincing.<br>
<div class="Ih2E3d"></div></blockquote><div><br>Tuples are immutable. Lists are mutable. The reason I suggest making it variable length is so that it supports the full list semantics and you could for example create a NamedList object with no values and then add values to it (ditto for NamedDict).<br>
 <br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d"><br>
> NamedDict(typename, fieldnames[, optionalfields[, verbose]])<br>
><br>
> Returns a new dict subclass named typename. The new subclass is used to<br>
> create dict-like objects that have fields accessible by attribute lookup as<br>
> well as supporting other dict operations. Instances of a NamedDict may be<br>
> created using keyword arguments only. If optionalfields is not true, then<br>
> the NamedDict must have a value for every field. If a NamedDict does not<br>
> contain a field, accessing it returns None when accessed by attribute (a.x)<br>
> and raises KeyError when accessed using by key (a['x']).<br>
<br>
</div>This one, though, doesn't sound any different from a struct-style<br>
class, except that it's easier (by ".__dict__") to access the class as<br>
a dictionary.  Hell, here you go:<br>
<br>
>>> class NamedDict(object):<br>
>>>   def __getitem__(self, key):<br>
>>>     return self.__dict__[key]<br>
</blockquote><div><br>Well, I think the "Named" aspect is important. For example, repr(x) tells you the name. I can tell if two objects are the same type.<br><br>Also the NamedDict supports attribute access to the specific fields you've declared and no others. So it's not a freewheeling object that you can add any attribute to it. It's just a lightweight way to create an object backed by a dict (just as NamedTuple is).<br>
<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
--<br>
Cheers,<br>
<font color="#888888">Leif<br>
</font></blockquote></div><br>