Finding attributes in a list
James Stroud
jstroud at mbi.ucla.edu
Sun Apr 3 00:51:09 EST 2005
On Saturday 02 April 2005 08:44 pm, Marcus Goldfish wrote:
> (2) The Player class looks like a nice model for a data table when one
> wants to sort by arbitrary column. Would you agree?
The Player class is (and any class) is absolutely fabulous when you have
heterogenous data (string, int, etc). I would not fall into the trap of LoDs
(Lists of Dictionaries). They get unwieldy because you always have to manage
them with functions. You end up writing a module built around your specific
dictionary and you end up with a duct-taped object oriented design anyway.
Classes are way better--so use them up front, even if you think that your
data structure will never be complicated enough to warrant a class. It
eventually will if it is worth a damn to begin with. Given this is a soccer
team we are talking about, it is definitely worth being a full fledged class.
However, if it were basketball...
> (3) Suppose one wished to construct a player list...[snip]
team = [Player(azip[0],azip[1],azip[2]) for azip in zip(names,attack,defense)]
You have to love listcomp.
Better (IMHO) would be
team = [Player(azip) for azip in zip(names,attack,defense)]
where a Player might come to life with
class Player(object):
def __init__(self, atup):
self.name, self.attack, self.defense = atup
BUT, even way better (again, IMHO) would be
ateam = Team(zip(names,attack,defense))
where team could be initialized by a tuple:
class Team(list):
def __init__(self, azip):
for azip in alist:
self.data.append(Player(atup))
James
--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
http://www.jamesstroud.com/
More information about the Python-list
mailing list