tag:blogger.com,1999:blog-11788780.post3907964020491577136..comments2023-12-29T13:22:33.104-08:00Comments on JJinuxLand: Python: dicts vs. classesjjinuxhttp://www.blogger.com/profile/03270879497119114175noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-11788780.post-76798134816718345962013-09-05T11:31:35.154-07:002013-09-05T11:31:35.154-07:00> I'd love to know how to make a list of cl...> I'd love to know how to make a list of classes searchable without having to iterate over each record in turn. Is there a way to do that easily or am I stuck with iterating over lists?<br /><br />Do it the same way databases do it. Create indexes of some sort, either via dicts or some sort of tree.jjinuxhttps://www.blogger.com/profile/03270879497119114175noreply@blogger.comtag:blogger.com,1999:blog-11788780.post-39139442862727968662013-09-05T11:25:00.918-07:002013-09-05T11:25:00.918-07:00Thanks for the great comments, yacc and Art!Thanks for the great comments, yacc and Art!jjinuxhttps://www.blogger.com/profile/03270879497119114175noreply@blogger.comtag:blogger.com,1999:blog-11788780.post-67568873488514955062013-09-01T07:46:31.854-07:002013-09-01T07:46:31.854-07:00If you want something that is "just a little ...If you want something that is "just a little bit" more than a dict, you can create a class derived from a dict. See UserDict http://docs.python.org/2/library/userdict.htmlArt Zemonhttp://www.cheerfulcurmudgeon.com/noreply@blogger.comtag:blogger.com,1999:blog-11788780.post-44538435817091288782013-08-31T10:00:58.648-07:002013-08-31T10:00:58.648-07:00Actually one can just subclass from named tuple, a...Actually one can just subclass from named tuple, alternatively verbose=True gives you the source to paste.<br /><br />Named tuples have a number of benefits, a central one being a tuple hence immutable. Add in some properties, and methods and you get a really nice object lite with a slight functional touch (as the value cannot be changed,which makes reasoning about the code so much easier)yacchttps://www.blogger.com/profile/14179043092836802541noreply@blogger.comtag:blogger.com,1999:blog-11788780.post-52086924994166558152013-08-29T16:38:32.805-07:002013-08-29T16:38:32.805-07:00Thanks for the comments, guys :)
I thought about ...Thanks for the comments, guys :)<br /><br />I thought about using namedtuples, but decided to just go ahead and write out a class. In retrospect, it was the right thing, but my code assumes the instances are mutable.<br /><br />The Bunch class is nice, but I think have the benefit of creating a class like this is to a) give it a name b) document what attributes you expect to be in it.<br /><br />It'd be nice if Python had something to let me create a simple class with a set of attributes really quickly, kind of like a case class in Scala.jjinuxhttps://www.blogger.com/profile/03270879497119114175noreply@blogger.comtag:blogger.com,1999:blog-11788780.post-21058345785137030662013-08-29T12:11:24.111-07:002013-08-29T12:11:24.111-07:00The advantage of a Bunch over a namedtuple is that...The advantage of a Bunch over a namedtuple is that a Bunch is mutable: you can add, remove, and modify attributes in-place. But namedtuple has a built-in order. Your choice.David Goodgerhttps://www.blogger.com/profile/06673699859175469861noreply@blogger.comtag:blogger.com,1999:blog-11788780.post-33942477491141402142013-08-29T11:52:11.636-07:002013-08-29T11:52:11.636-07:00For a simple collection of data without specialize...For a simple collection of data without specialized behavior, I also use namedtuple, or a <a href="http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/?in=user-97991" rel="nofollow">"Bunch" class</a> (see the comments for variations). But add in custom behavior (methods), and of course a class is just the ticket. (Of course, namedtuple is just a quick and dirty way to implement a minimal-behavior class. Look up its implementation in the collections module.)<br /><br />The nice thing about namedtuple (and Bunch) is that when behavior needs to be added, the namedtuple definition can easily be replaced by a class definition, without affecting the rest of the code.David Goodgerhttps://www.blogger.com/profile/06673699859175469861noreply@blogger.comtag:blogger.com,1999:blog-11788780.post-3441595354606359272013-08-29T10:14:26.726-07:002013-08-29T10:14:26.726-07:00I'd use namedtuple for that. But maybe that&#...I'd use namedtuple for that. But maybe that's just me.Kevin Hhttps://www.blogger.com/profile/03013155163736662271noreply@blogger.com