By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

get a list from a string

P: n/a
Hi,

I have a string "distances = [[1,1,1,1],[2,2,2,2]]". I want to create a
variable called distances whose value is the list [[1,1,1,1],[2,2,2,2]]. How can
I go about that? I know I can use setattr, but how do I create the list from the
string?

Regards,
Simon

Jun 7 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Jun 7, 3:34 am, simon kagwe <simonka...@yahoo.comwrote:
Hi,

I have a string "distances = [[1,1,1,1],[2,2,2,2]]". I want to create a
variable called distances whose value is the list [[1,1,1,1],[2,2,2,2]]. How can
I go about that? I know I can use setattr, but how do I create the list from the
string?

Regards,
Simon
exec("distances = [[1,1,1,1],[2,2,2,2]]")

~Sean

Jun 7 '07 #2

P: n/a
exec("distances = [[1,1,1,1],[2,2,2,2]]")
Wow! So simple!

Thanks a lot. :-)


Jun 7 '07 #3

P: n/a
On Jun 7, 6:06 am, simon kagwe <simonka...@yahoo.comwrote:
exec("distances = [[1,1,1,1],[2,2,2,2]]")
To be clear, exec is *not* a function; it's a statement. That means it
can't be used in lambda functions, for example.
Wow! So simple!
but dodgy, as it'll execute any python code.
Thanks a lot. :-)
Jun 7 '07 #4

P: n/a
On Thu, 07 Jun 2007 11:06:54 +0000, simon kagwe wrote:
>exec("distances = [[1,1,1,1],[2,2,2,2]]")
Wow! So simple!

Thanks a lot. :-)
Yes, and when you embed this in your web-application, using data gathered
from a web-form, the black-hat hackers will thank you for the security
hole too.

Surely a much better solution would be NOT to start with a string like
"distances = [[1,1,1,1],[2,2,2,2]]" in the first place? Where does that
string come from? If it comes from the user, at run-time, using exec is a
MAJOR security hole. If it comes from the source code, then WHY???

I wish exec and eval were hidden away in a module so they were harder (but
not impossible) to get to. Because I'm paranoid, I wish importing that
module would print an warning saying "Are you MAD??? Don't do this!!!". I
wish even more that Python would come with a built-in "make a list from a
list representation" function, but that at least is fairly easy to create:
you can modify


Here is a discussion about just how hard (that is, probably impossible) it
is to make eval safe:

http://effbot.org/zone/librarybook-core-eval.htm


Jun 7 '07 #5

P: n/a
Ah, sorry, pre-mature sending. Stupid keyboard accelerators :(

To finish the sentence I was trying to write:

On Thu, 07 Jun 2007 22:39:41 +1000, Steven D'Aprano wrote:
I wish even more that Python would come with a built-in "make a list from a
list representation" function, but that at least is fairly easy to create:
you can modify
.... Fredrik Lundh's safe-eval function:

http://online.effbot.org/2005_11_01_archive.htm


--
Steven.

Jun 7 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.