On Fri, 5 Mar 2010 17:38:08 -0800
Daryl V <[email protected]> wrote:
> I have a csv list of data, of the form:
> plot, utmN83_X, utmN83_Y, plot_radius_m
> Spring1,348545,3589235,13.2
> etc.
[...]
> What I want to do is use the first entry in that row (row[0]) as the
> variable name for the instantiated class.
There are several solution, for design & implementation.
(1) You can do some trick to create vars as you explain. But it's ugly and in
my opinion wrong: because the set of data build a kind of whole, thus should
have global name, say "data".
(2) Build a dictionary which keys are the names:
name = row[0]
data[name] = value
...
v = data[name]
This is much better because its standard programming and the data are properly
packed. Right?
(3) Build a custom composite object which attributes are named the way you
want. Python does not have a syntax to set attributes with variable names, but
provides a builtin func for this:
name = row[0]
setattr(data, name, value)
...
v = data.name
It may look a bit contorsed, but again it's because python does not have a
syntax for this.
I would go for the latter -- it may be a question of style.
Denis
--
________________________________
la vita e estrany
spir.wikidot.com
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor