我試圖讓在Python的腳本,在執行時,要求一個函數名的用戶和打印功能.__doc__
例如:詢問用戶的函數名,然後打印.__ doc__會給出,不工作
>>> print abs.__doc__
abs(number) -> number
Return the absolute value of the argument.
問題是,它不適用於raw_input。以下是我的代碼,它在執行時會發生什麼。 代碼:
f = raw_input("Your function: ")
print f.__doc__
執行:
Your function: abs
str(object='') -> string
Return a nice string representation of the object.
If the argument is a string, the return value is the same object.
我在做什麼錯?
'f'是一個字符串對象,而不是一個函數對象。 – cdarke
您可以使用'__builtins __.__ dict __ [f] .__ doc__'。但是,只適用於內置函數:) – MSeifert
你對abs'.__ doc__'有什麼期望? – jonrsharpe