Fwd: [Tutor] variable from input?

Lloyd Hugh Allen chandrakirti at gmail.com
Tue Oct 26 04:01:48 CEST 2004


Forgot to hit the "a" key...


---------- Forwarded message ----------
From: Lloyd Hugh Allen <chandrakirti at gmail.com>
Date: Mon, 25 Oct 2004 22:01:26 -0400
Subject: Re: [Tutor] variable from input?
To: Glen Wheeler <gew75 at hotmail.com>


Perhaps the variable will later be a class with attributes describing
a person, and it will make sense for each person to be her/his own
variable? I could see that.

You can maybe even be kind of sort of safe if you can ensure that "s"
is composed solely of alphanumeric characters (parens and equals are
definitely dangerous; space would probably provoke an exception)...




On Tue, 26 Oct 2004 10:48:28 +1000, Glen Wheeler <gew75 at hotmail.com> wrote:
>
>   Howdy.
>
>   Firstly, I cannot think of any reason why you would want to do this
> instead of simply having a ``name'' variable holding that person's name.
>
>   Anyway, the following would work:
>
> >>> s = raw_input("Don't try this at home...")
> >>> exec(s+" = ''")
> >>> fred
> ''
> >>>
>
>   But don't do it...
>
>   Glen
>
>
>
> ----- Original Message -----
> From: "Liam Clarke" <cyresse at gmail.com>
> To: "Python Tutor" <tutor at python.org>
> Sent: Tuesday, October 26, 2004 10:12 AM
> Subject: Re: [Tutor] variable from input?
>
> > That's an interesting one...
> >
> > You could have the variable contained in a separate module, and then
> > when the user enters the variable name, have another function write
> > that variable name throughout the other module, and then reimport the
> > other module, but that would only work if the variable were entirely
> > contained within that module.
> >
> > But I've wondered that myself, but the implementation doesn't seem to
> > be worth the effort.
> > Once again, I could be entirely wrong.
> >
> > Liam
> >
> >
> > On Mon, 25 Oct 2004 19:53:22 -0400, Rene Lopez <renx99 at gmail.com> wrote:
> >> is it possible to have the user input something with a raw_input
> >> command, and then base upon the input make that into the name of a
> >> variable?
> >>
> >> for example:
> >>
> >> answer = raw_input("what is your name?")
> >>
> >> user types in Rene, and some how we end up with a variable named Rene?
> >> --
> >>
> >> Rene
> >> _______________________________________________
> >> Tutor maillist  -  Tutor at python.org
> >> http://mail.python.org/mailman/listinfo/tutor
> >>
> >
> >
> > --
> > 'There is only one basic human right, and that is to do as you damn well
> > please.
> > And with it comes the only basic human duty, to take the consequences.
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list