L
longqian9509
In pyhton 3.1, I found the following code will succeed with argument 1
to 4 and fail with argument 5 to 9. It is really strange to me. I
suspect it may be a buy in exec() function. Does anyone have some idea
about it? Thanks.
t1="""
class foo:
def fun():
print('foo')
def main():
global foo
foo.fun()
main()
"""
t2="""
class foo:
def fun():
print('foo')
def main():
foo.fun()
main()
"""
import sys
import copy
if sys.argv[1]=='1':
exec(t1)
elif sys.argv[1]=='2':
exec(t2)
elif sys.argv[1]=='3':
exec(t1,{},{})
elif sys.argv[1]=='4':
exec(t2,globals(),locals())
elif sys.argv[1]=='5':
exec(t2,{},{})
elif sys.argv[1]=='6':
exec(t2,globals(),{})
elif sys.argv[1]=='7':
exec(t2,{},locals())
elif sys.argv[1]=='8':
exec(t2,copy.copy(globals()),locals())
elif sys.argv[1]=='9':
exec(t2,globals(),copy.copy(locals()))
to 4 and fail with argument 5 to 9. It is really strange to me. I
suspect it may be a buy in exec() function. Does anyone have some idea
about it? Thanks.
t1="""
class foo:
def fun():
print('foo')
def main():
global foo
foo.fun()
main()
"""
t2="""
class foo:
def fun():
print('foo')
def main():
foo.fun()
main()
"""
import sys
import copy
if sys.argv[1]=='1':
exec(t1)
elif sys.argv[1]=='2':
exec(t2)
elif sys.argv[1]=='3':
exec(t1,{},{})
elif sys.argv[1]=='4':
exec(t2,globals(),locals())
elif sys.argv[1]=='5':
exec(t2,{},{})
elif sys.argv[1]=='6':
exec(t2,globals(),{})
elif sys.argv[1]=='7':
exec(t2,{},locals())
elif sys.argv[1]=='8':
exec(t2,copy.copy(globals()),locals())
elif sys.argv[1]=='9':
exec(t2,globals(),copy.copy(locals()))