Re: exec inside functions in Python 3

2016-03-22 Thread Chris Angelico
On Tue, Mar 22, 2016 at 11:57 PM, Steven D'Aprano wrote: > Anyone have any idea what is going on here? > > > def test(): > spam = 1 > exec("spam = 2; print('inside exec: %d' % spam)") > print('outside exec: %d' % spam) > > > In Python 2.7: > > py> test() > inside exec: 2 > outside exec

exec inside functions in Python 3

2016-03-22 Thread Steven D'Aprano
Anyone have any idea what is going on here? def test(): spam = 1 exec("spam = 2; print('inside exec: %d' % spam)") print('outside exec: %d' % spam) In Python 2.7: py> test() inside exec: 2 outside exec: 2 In Python 3.4: outside exec: 1 py> test() inside exec: 2 outside exec: 1