Re: [Tutor] Addressing a variable whose name is the value of a string

2007-04-09 Thread Andreas Pfrengle
Kent Johnson wrote: > Andreas Pfrengle wrote: > >> # Accessing db (see http://www.djangoproject.com/documentation/db-api/): >> myobj = MyModel.objects.get() >> var = myobj.vector >> # vector is a Textfield, so var now contains a string, naming another >> db-field of myobj >> >> execstr = "attr =

Re: [Tutor] Addressing a variable whose name is the value of a string

2007-04-09 Thread Kent Johnson
Andreas Pfrengle wrote: > # Accessing db (see http://www.djangoproject.com/documentation/db-api/): > myobj = MyModel.objects.get() > var = myobj.vector > # vector is a Textfield, so var now contains a string, naming another > db-field of myobj > > execstr = "attr = myobj." + var > exec execstr >

Re: [Tutor] Addressing a variable whose name is the value of a string

2007-04-09 Thread Andreas Pfrengle
Bob Gailer wrote: > Andreas Pfrengle wrote: > >> [snip] > >> looks good if I'm happy with my values inside mydict and don't want >> to have sth. like x=5 in the end. But since 'x' is the name of a >> database field (I simplified it here for an example), I still see no >> way around the exec, so

Re: [Tutor] Addressing a variable whose name is the value of a string

2007-04-09 Thread Jordan Greenberg
Andreas Pfrengle wrote: > Bob Gailer wrote: > >> Andreas Pfrengle wrote: >> >>> Hello, >>> >>> I want to change the value of a variable whose name I don't know, but >>> this name is stored as a string in another variable, like: >>> >>> x = 1 >>> var = 'x' >>> >>> Now I want to change the value of

Re: [Tutor] Addressing a variable whose name is the value of a string

2007-04-08 Thread Kent Johnson
John Clark wrote: > locals()[var] > > But I am not sure what the pros/cons for doing something like this would > be... locals() should be considered read-only. From the docs: locals( ) Update and return a dictionary representing the current local symbol table. Warning: The content

Re: [Tutor] Addressing a variable whose name is the value of a string

2007-04-08 Thread Kent Johnson
Andreas Pfrengle wrote: > Bob Gailer wrote: >>> Now I want to change the value of x, but address it via var. >> exec is the statement for doing this, but the need to do this can >> always be met better by using a dictionary instead of global variables. >> > Thanks Bob, the 'exec' saved me. But I

Re: [Tutor] Addressing a variable whose name is the value of a string

2007-04-08 Thread Bob Gailer
Andreas Pfrengle wrote: > [snip] > looks good if I'm happy with my values inside mydict and don't want to > have sth. like x=5 in the end. But since 'x' is the name of a database > field (I simplified it here for an example), I still see no way around > the exec, so I can change the content of

Re: [Tutor] Addressing a variable whose name is the value of a string

2007-04-08 Thread Andreas Pfrengle
Jordan Greenberg wrote: Andreas Pfrengle wrote: Bob Gailer wrote: Andreas Pfrengle wrote: Hello, I want to change the value of a variable whose name I don't know, but this name is stored as a string in another variable, like: x = 1 var = 'x' Now I want to change the value

Re: [Tutor] Addressing a variable whose name is the value of a string

2007-04-08 Thread Andreas Pfrengle
Bob Gailer wrote: > Andreas Pfrengle wrote: > >> Hello, >> >> I want to change the value of a variable whose name I don't know, but >> this name is stored as a string in another variable, like: >> >> x = 1 >> var = 'x' >> >> Now I want to change the value of x, but address it via var. > > exec is

Re: [Tutor] Addressing a variable whose name is the value of a string

2007-04-08 Thread John Clark
Bob Gailer wrote: >Andreas Pfrengle wrote: >> Hello, >> >> I want to change the value of a variable whose name I don't know, but >> this name is stored as a string in another variable, like: >> >> x = 1 >> var = 'x' >> >> Now I want to change the value of x, but address it via var. > >exec is the

Re: [Tutor] Addressing a variable whose name is the value of a string

2007-04-08 Thread Bob Gailer
Andreas Pfrengle wrote: > Hello, > > I want to change the value of a variable whose name I don't know, but > this name is stored as a string in another variable, like: > > x = 1 > var = 'x' > > Now I want to change the value of x, but address it via var. exec is the statement for doing this, but t

[Tutor] Addressing a variable whose name is the value of a string

2007-04-08 Thread Andreas Pfrengle
Hello, I want to change the value of a variable whose name I don't know, but this name is stored as a string in another variable, like: x = 1 var = 'x' Now I want to change the value of x, but address it via var. I'm quite sure I've already seen a solution for this, but right now I don't get it