1
我想使用IPython配置系統爲我自己的類提供配置參數。我嘗試了一個簡單的測試。IPython自定義類可配置
from IPython.config.configurable import Configurable
from IPython.utils.traitlets import Int, Unicode, Bool
class MyConfigTest(Configurable):
user_name = Unicode(u'Fred', config=True)
def __init__(self, config=None):
super(MyConfigTest, self).__init__(config=config)
def display(self):
print 'my name is ' + self.user_name
然後把這個在我的配置文件:
c.MyConfigTest.user_name = 'John'
當我運行IPython的--debug我可以看到我的配置變量被正確讀取:
[TerminalIPythonApp] Config changed:
[TerminalIPythonApp] {'MyConfigTest': {'user_name': 'John'}, 'TerminalIPythonApp
': {'log_level': 10}}
但當我在我的代碼中實例化MyConfigTest
時,它仍然具有默認值:
In [1]: x = MyConfigTest()
In [2]: x
Out[2]: <__main__.MyConfigTest at 0x2e1dc50>
In [3]: x.display()
my name is Fred
我在錯誤地讀取變量嗎?我閱讀了一些IPython源代碼,但看不到任何明顯的東西。