我有一個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