evaluation question

Muttley at dastardlyhq.com Muttley at dastardlyhq.com
Sat Jan 28 05:11:38 EST 2023


On Fri, 27 Jan 2023 21:04:58 +0000
Ben Bacarisse <ben.usenet at bsb.me.uk> wrote:
>Muttley at dastardlyhq.com writes:
>
>> Hi
>
>It looks like you posted this question via Usenet.  comp.lang.python is
>essentially dead as a Usenet group.  It exists, and gets NNTP versions
>of mail sent to the mailing list, but nothing posted to the group via
>NNTP get send on the mailing list.  I prefer Usenet and dislike mailing
>lists but that just means I can't really contribute to this "group"
>
>The "python-list" an an excellent resource (if you like the email
>interface) and you can subscribe here:
>
>https://mail.python.org/mailman/listinfo/python-list>,
>
>> 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?
>
>It handles only expressions, and "for i in range(1,10): i" is not an
>expression.  You can use
>
>>>> exec("for i in range(1,10): i")

Ok, thanks.



More information about the Python-list mailing list