Python 3: exec arg 1

Rob Williscroft rtw at freenet.co.uk
Sun Jan 18 12:06:10 EST 2009


Steven D'Aprano wrote in news:018342f9$0$8693$c3e8da3 at news.astraweb.com
in comp.lang.python: 

> I'm not sure if this is a stupid question or not, but what's a 
> TextIOWrapper? In the example you give:
> 
> exec(open(fname))
> 
> the argument to exec -- open(fname) -- is a file object:
> 
>>>> type(open('hello.py'))
> <type 'file'>
> 
> 
> BTW, exec is a statement. The brackets there are totally superfluous.
> You can, and should, write: 
> 
> exec open(fname)
> 

You must have missed the subject line: "Re: Python 3: exec arg 1"

Python 3.0 (r30:67507, Dec  3 2008, 19:44:23) [MSC v.1500 64 bit (AMD64)] 
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> open( "hello.py" )
<io.TextIOWrapper object at 0x000000000212F7F0>
>>> exec "a = 1"
  File "<stdin>", line 1
    exec "a = 1"
               ^
SyntaxError: invalid syntax
>>> exec( "a = 1" )
>>> a
1
>>>

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list