Re: Convert the contents of a string into name of variable
Erwan VITIERE a écrit : Hello, I want to convert the contents of a string into name of variable. For example: var1=toto ... toto=5 print toto exec toto = 5 print toto But I would use another solution if possible. -- http://mail.python.org/mailman/listinfo/python-list
Convert the contents of a string into name of variable
Hello, I want to convert the contents of a string into name of variable. For example: var1=toto ... toto=5 print toto -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert the contents of a string into name of variable
Erwan VITIERE wrote: I want to convert the contents of a string into name of variable. For example: var1=toto ... toto=5 print toto why? Python works better if you use it to write Python code. the Python solution is to use a dictionary: key1 = toto data = {} data[toto] = 5 print data[key1] /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert the contents of a string into name of variable
Because i don't want to use this syntax because it is too long, I want a direct access : Not : DicoUser['TOTO'].DicoTable.['MY_TABLE'].DicoLabel.['MY_LABEL'] = for exemple Finally, i want to use : TOTO.MY_TABLE.MY_LABEL = for exemple Fredrik Lundh [EMAIL PROTECTED] a écrit dans le message de news: [EMAIL PROTECTED] Erwan VITIERE wrote: I want to convert the contents of a string into name of variable. For example: var1=toto ... toto=5 print toto why? Python works better if you use it to write Python code. the Python solution is to use a dictionary: key1 = toto data = {} data[toto] = 5 print data[key1] /F -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert the contents of a string into name of variable
Erwan VITIERE wrote: Because i don't want to use this syntax because it is too long, I want a direct access : Not : DicoUser['TOTO'].DicoTable.['MY_TABLE'].DicoLabel.['MY_LABEL'] = for exemple Finally, i want to use : TOTO.MY_TABLE.MY_LABEL = for exemple setattr might be what you are looking for -- http://mail.python.org/mailman/listinfo/python-list
Re: Convert the contents of a string into name of variable
TRy db_row does exactly what you want to do. Slower, but more simple: ## #Sequence2Struct.py class Struct: pass def MakeStruct(seq, names): obj = Struct() if len(seq) != len(names): raise IndexError(seq and names are not the same length) for i in range(len(names)): obj.__dict__[names[i]] = seq[i] return obj def ExtractNames(t): return [item[0] for item in t] if __name__ == __main__: t = (1, 2, 3) n1 = ((A, 1), (B, 1), (C, 2)) n = ExtractNames(n1) print n s = MakeStruct(t, n) print s, s.A, s.B, s.C n = (A, B) s1 = MakeStruct(t, n) ## -- http://mail.python.org/mailman/listinfo/python-list