如和將class中定義的變量打印或讀取出來,受maskrcnn的config.py的啟示,我將對該函數進行解釋。
我將介紹該函數前,需要對一些名詞進行解釋,如下:
①Ipython:ipython是一個python的交互式shell,比默認的python shell好用得多,支持變量自動補全,自動縮進,支持bash shell命令,內置了許多很有用的功能和函數。學習ipython將會讓我們以一種更高的效率來使用python。同時它也是利用Python進行科學計算和交互可視化的一個最佳的平台。
②dir():函數不帶參數時,返回當前范圍內的變量、方法和定義的類型列表;帶參數時,返回參數的屬性、方法列表。如果參數包含方法__dir__(),該方法將被調用。如果參數不包含__dir__(),該方法將最大限度地收集參數信息。如下返回屬性變量:
③.startswith():此函數判斷一個文本是否以某個或幾個字符開始,結果以True或者False返回。
text='welcome to qttc blog'
print text.startswith('w') # True
print text.startswith('wel') # True
print text.startswith('c') # False
print text.startswith('') # True
順道介紹endswith():此函數判斷一個文本是否以某個或幾個字符結束,結果以True或者False返回。
text='welcome to qttc blog'
print text.endswith('g') # True
print text.endswith('go') # False
print text.endswith('og') # True
print text.endswith('') # True
print text.endswith('g ') # False
④getattr(self, a):返回一個對象屬性值,即變量的值
def display(self):
"""Display Configuration values."""
print("\nConfigurations:")
for a in dir(self): # dir() 函數不帶參數時,返回當前范圍內的變量、方法和定義的類型列表;帶參數時,返回參數的屬性、方法列表
if not a.startswith("__") and not callable(getattr(self, a)): # getattr() 函數用於返回一個對象屬性值
print("{:30} {}".format(a, getattr(self, a)))
print("\n")
在maskrcnn中,執行此代碼運行結果為: