evaluation question

Muttley at dastardlyhq.com Muttley at dastardlyhq.com
Sat Jan 28 05:13:00 EST 2023


On Sat, 28 Jan 2023 14:22:01 +1300
dn <PythonList at DancesWithMice.info> wrote:
>On 28/01/2023 05.37, Muttley at dastardlyhq.com wrote:
>> This is probably a dumb newbie question but I've just started to learn
>> python3 and eval() isn't behaving as I'd expect in that it works for
>> some things and not others. eg:
>> 
>>>>> eval("1+1")
>> 2
>>>>> eval("print(123)")
>> 123
>>>>> eval("for i in range(1,10): i")
>> Traceback (most recent call last):
>>    File "<stdin>", line 1, in <module>
>>    File "<string>", line 1
>>      for i in range(1,10): i
>>        ^
>> SyntaxError: invalid syntax
>> 
>> Why did the 3rd one fail? Does it not handle complex expressions?
>
>eval() is very powerful, and therefore rather dangerous in the risks it 
>presents.
>
>Thus, seems a strange/advanced question for a "newbie" to be asking. YMMV!

Well ok, new-ish :)

>Do you know about the Python REPL?

Haven't learnt the acronyms yet.

>If you open python within a terminal, each of the three 
>expressions/compound-statements listed will work, as desired, without 
>eval().

Umm, yeah, thats kind of obvious isn't it?



More information about the Python-list mailing list