Hi,

Recently I tried to execute a python file using execfile (exec performed
just the same for that reason).
I encountered the behavior below:

"""
$ cat execme.py
a = 2
$ python
Python 2.4.3 (#1, May 24 2008, 13:57:05)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def execfile_func():
...     execfile('execme.py')
...     print 'locals() = %s' % str(locals())
...     print a
...
>>> execfile_func()
locals() = {'a': 2}
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 4, in execfile_func
NameError: global name 'a' is not defined
>>>
"""

After execfile, the a variable can be found in locals(), however any direct
reference (e.g., print a) fails.
Is it expected?

Thanks,
Doron
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to