我有一个GUI,它允许用户从特定的* .py文件运行任何函数。我希望一些功能以不同的方式运行。为了做到这一点,我试图给函数附加属性(简单的东西,比如它需要的输入)。但是,我发现获得这些属性的唯一方法是首先运行代码。获取python函数属性而不运行函数
有没有办法在不运行代码的情况下获得这些属性,或者可能是一种通常更接近这个任务的pythonic方法?
我的代码非常简陋的例子:
FileA.py
def Beta(x):
Beta.input_stype = "Float"
y = x + 0.5
return y
def Gamma(x):
Gamma.input_stype = "String"
y = x + "_blah_blah_blah"
return y
def Delta(x):
Delta.input_stype = "String"
y = x.index('WhereIsIt')
return y
FileB.py
import FileA
import inspect
z = inspect.getmembers(Fiddle2, inspect.isfunction)
#### User selects the code value here ####
x = user_selection
executable = z[x][1] # Pulls the executable code
if executable.input_stype == "Float" :
y = executable(45)
elif executable.input_stype == "String" :
y = executable('Testing_the_WhereIsIt_stuff')
的[Python中的参数类型检查(http://stackoverflow.com/questions/734368/type-checking-of-arguments-python) – Natecat