Variables from variables

Alex Martelli aleaxit at yahoo.com
Fri Aug 17 12:39:49 EDT 2001


<paul at nlhas.no> wrote in message
news:2gppnt4vfvt570kivgsgc99e5fca7ckm3b at 4ax.com...
> I've come to a dead end with this one, and would be grateful for
> suggestions. Scenario - a tuple with field names (self.description) to
> be mapped to class variables and assigned values from self.record.
    ...
> y = 0
> r=self.record[0]
> for x in self.description :
> k = x[0]

This means k is the first character of string x -- I don't
understand why you want that?

> c = r[y]
> y += 1
> v = 'self.' + k
> exceptiontype = ''
> try :
>    cde = compile( "%s = %s" % (v , c) , '<string>' , 'exec')
>            exec cde
>            print 'OK ' +  v + ' ' + `type(v)`
> except StandardError , exceptiontype:
>    print `exceptiontype` + v + ' ' + `type(v)`

I'm not following in detail, but you seem to mean something
like:
    setattr(self, k, c)


Alex






More information about the Python-list mailing list