[Tutor] Fwd: The why

Luke Paireepinart rabidpoobear at gmail.com
Thu Jul 16 09:18:18 CEST 2009

You need to read what dictionaries are.
Essentially, any time you think "oh I need to keep separate variables
for these values, but I won't know what their names are until
runtime!" the correct answer is to not try to manipulate them into
variables, but to use them as dictionary keys, or organize them into
some other data structure that you can recall later.

Quick example:
x = {}
import random
while 1:
    name = raw_input("Name? ")
    if name.strip() == 'quit':
    x[name] = random.randrange(100)
print x

Name? bob
Name? jack
Name? thomas
Name? quit
{'thomas': 39, 'bob': 69, 'jack': 42}

On Wed, Jul 15, 2009 at 9:34 PM, Rich Lovely<roadierich at googlemail.com> wrote:
> ---------- Forwarded message ----------
> From: chris Hynes <cjhynes36 at hotmail.com>
> Date: 2009/7/15
> Subject: The why
> To: roadierich at googlemail.com
> Well, I'm trying to create an interactive program, let's say I'm
> running the program, I ask the user to give the array a name, I then
> do some computations and store the results in that array. While I'm
> still running the program I might decide to create several more arrays
> of the same type/dimension with names of my choosing same types of
> computation. When I'm done, I might have several arrays created, all
> with different names that I have given them. In the ipython mode I
> could call up any of these arrays off and on again as I want to
> visually compare them to each other.
> I mean I guess what I could do is go back to my code, name the array
> Chris, let a calculation use x=2, save, run the program, go back in to
> the code, edit it so that the array is named Bob along with changing
> x=3, save, run the program a second time. When I'm done I'll now have
> two arrays, one named Chris the other Bob with different results
> because I changed some other input paramenters.
> I just now thought of maybe making one huge array, storing all the
> results of x=2 and use a conditional satement like if x=2, then name
> was Chris. But it would be so much easier if I could type print Chris
> or print Bob and it woulds spit out what those arrays were.
>> From: roadierich at googlemail.com
>> Date: Wed, 15 Jul 2009 16:33:00 +0100
>> Subject: Re: [Tutor] objects becoming pointers
>> To: cjhynes36 at hotmail.com
>> CC: tutor at python.org
>> 2009/7/15 chris Hynes <cjhynes36 at hotmail.com>:
>> > I guess I have to start somewhere to ask............
>> >
>> > I want the user to input a name, say "Chris". I know I can use the code:
>> >
>> > name=raw_input()
>> >
>> > I now want:
>> >
>> > "Chris"=zeros((3,3))
>> >
>> > so that when I type:
>> >
>> > print Chris
>> >
>> > the return will be an array of zero's 3x3
>> >
>> > So that I can understand this deeper, I know that "name" is just a pointer
>> > to the object "Chris". I don't want to just change the pointer to something
>> > else, like "zeros((3,3))" but I want to make "Chris" become the pointer or
>> > the name of the pointer. At least that's what I think I want.
>> >
>> > ________________________________
>> > Windows Live™ SkyDrive™: Get 25 GB of free online storage. Get it on your
>> > BlackBerry or iPhone.
>> > _______________________________________________
>> > Tutor maillist  -  Tutor at python.org
>> > http://mail.python.org/mailman/listinfo/tutor
>> >
>> >
>> Sorry if I've sent this twice...
>> Why would you want to do that?
>> The closest you can get to that is using exec, but exec is usually
>> considered a code smell. I'd say you're trying to do the wrong thing.
>> --
>> Rich "Roadie Rich" Lovely
>> There are 10 types of people in the world: those who know binary,
>> those who do not, and those who are off by one.
> ________________________________
> Windows Live™: Keep your life in sync. Check it out.
> --
> Rich "Roadie Rich" Lovely
> There are 10 types of people in the world: those who know binary,
> those who do not, and those who are off by one.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list