Loading functions from a file during run-time

Wensheng wenshengwang at gmail.com
Wed Feb 16 12:47:47 EST 2005


#--- file bar.py
def negate(n):
	return -n

def square(n):
	return n*n
#--- end bar.py

>>> foo="bar"
>>> fs=__import__(foo)
>>> import types
>>> f=[a for a in dir(fs) if a[0:2]!='__' and
type(getattr(fs,a))==types.FunctionType]
>>> f
['negate', 'square']
>>> n=5
>>> exec("print fs."+f[0]+"(n)")
-5
>>> exec("print fs."+f[1]+"(n)")
25


Isn't the problem solved?




More information about the Python-list mailing list