[Tutor] Mysterious Qualifier!

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 15 Feb 2002 13:38:33 -0800 (PST)


On Fri, 15 Feb 2002, Fred Allen wrote:

> Despite reviewing the documentation, I seem unable to find how to correct
> the syntax error presented beneath the following code block. I'm using 2.=
2.
>=20
> def lsdoc(modu):
> =09exec 'reload ' + modu
> =09md =3D eval(modu + ".__doc__")
> =09print 'Module ' + str(modu) + "'s Document String:", md

> =93SyntaxError: unqualified exec is not allowed in function 'lsdoc' it
> contains a nested function with free variables (lsmoddocs.py, line 8)=94


You may need to make a small correction on the exec():

###
exec 'reload(%s)' + modu
###

I think the error message about "free variables" is related to the
missing parens there.



By the way, we can "qualify" an exec by doing something like this:

###
>>> exec 'print x + y' in {'x' : 3, 'y' : 4}
7
###

The dictionary we pass will be the place that Python uses to look up
variable names and values.