exec "x = 3; print x" in a - How does it work?

Chris Angelico rosuav at gmail.com
Wed Mar 9 01:34:42 EST 2016


On Wed, Mar 9, 2016 at 5:25 PM, Veek. M <vek.m1234 at gmail.com> wrote:
> ah, okay - i'm familiar with the py3 syntax where you do:
> eval('whatever stmt', globals={}, locals={})
> I suppose this: exec " " in NS; syntax is strictly py2?
>
> Many thanks :)

Yeah, that's one of the things that was cleaned up in Py3. Several
pieces of magic syntax were replaced with perfectly ordinary
functions. While there are plenty of people who complain about having
to put parentheses around their print calls, I'm firmly of the opinion
that print(..., file=somefile) is WAY better than the >> syntax that
Py2 used. I can never remember whether it has to go first or has to go
last, and whether it's >> or >>>, etc, etc. It's magic syntax. Py3?
It's a keyword argument. Easy!

ChrisA



More information about the Python-list mailing list