[Tutor] Python type confusion?
Danny Yoo
dyoo at hashcollision.org
Tue Sep 29 01:41:47 CEST 2015
> As C Smith notes, raw_input() returns a string. As the name suggests,
> it treats its input as raw text, and does not try to interpret it as
> data.
Whoops! I slightly misspoke here: I mean to say that it does not try
to interpret it as *structured* data.
That is, we want things that look like numbers to be treated as
numbers. Likewise, we'd like a string that looks like a list of
numbers to be treated as a list of numbers. That's the role of a
parser, and why we need to do something, such as using the
json.loads() function.
Others on the list might suggest instead using input() instead of
raw_input(), which will try to interpret what you enter in as if it
were a Python expression. Effectively, this will also parse the text
into structured values. However, although this is a straightforward
way to make your program work, I don't think it's a good approach
because input() has a hidden use of the infamous "eval()" function
embedded in it. Certain inputs to input() can cause your program to
do weird things due to its use of eval(), so I think it's best to
avoid it.
Dunno if you're familiar with all the problems with eval(); if you are
interested, ask, and I'm sure there will be a lot of response from the
list.
Best of wishes to you!
More information about the Tutor
mailing list