1
當我使用PythonNet從Python加載.Net類時,它不會爲類填充python __ dict__,但是當我使用ipython加載它時同樣的命令序列也是如此。任何人都可以說如何從普通的Python獲得ipython行爲?PythonNet - 如何填充__dict__ - ipython呢,python不會
非常感謝。
python -c "import clr; clr.AddReference('System.Windows.Forms'); import System.Windows.Forms; print len(System.Windows.Forms.__dict__)"
3
ipython -c "import clr; clr.AddReference('System.Windows.Forms'); import System.Windows.Forms; print len(System.Windows.Forms.__dict__)"
714
我試過這個在python 2.7.11和3.5.2。 Pythonnet 2.0.0和2.1.0。我相信我正在使用CLR 4.0.30319(這是與其他人一起安裝的)。
我認爲ipython正在做某種內省/反射來查找Forms類/模塊的成員,可能是因爲它的完成/ intellisense。我如何在普通的Python中調用它?
這裏有一個更長的版本,打印的內容(由denfromufa的要求) - 調用文件TestPN.py:
import clr
clr.AddReference('System.Windows.Forms')
import System.Windows.Forms
nCount = 0
for s, o in System.Windows.Forms.__dict__.items():
print(s, o, type(o))
nCount += 1
if nCount > 10:
break
和輸出從每個:
ipython TestPN.py
SelectionRange <class 'System.Windows.Forms.SelectionRange'> <class 'CLR.CLR Metatype'>
PropertyGrid <class 'System.Windows.Forms.PropertyGrid'> <class 'CLR.CLR Metatype'>
DataGridViewCellStyleConverter <class 'System.Windows.Forms.DataGridViewCellStyleConverter'> <class 'CLR.CLR Metatype'>
PrintPreviewControl <class 'System.Windows.Forms.PrintPreviewControl'> <class 'CLR.CLR Metatype'>
BindingManagerDataErrorEventArgs <class 'System.Windows.Forms.BindingManagerDataErrorEventArgs'> <class 'CLR.CLR Metatype'>
KeyPressEventArgs <class 'System.Windows.Forms.KeyPressEventArgs'> <class 'CLR.CLR Metatype'>
TableLayoutPanelCellPosition <class 'System.Windows.Forms.TableLayoutPanelCellPosition'> <class 'CLR.CLR Metatype'>
TreeViewImageIndexConverter <class 'System.Windows.Forms.TreeViewImageIndexConverter'> <class 'CLR.CLR Metatype'>
DrawListViewSubItemEventArgs <class 'System.Windows.Forms.DrawListViewSubItemEventArgs'> <class 'CLR.CLR Metatype'>
ItemChangedEventArgs <class 'System.Windows.Forms.ItemChangedEventArgs'> <class 'CLR.CLR Metatype'>
ToolStripItemRenderEventArgs <class 'System.Windows.Forms.ToolStripItemRenderEventArgs'> <class 'CLR.CLR Metatype'>
python TestPN.py
__name__ System.Windows.Forms <class 'str'>
__doc__ Namespace containing types from the following assemblies:
- System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
- System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
<class 'str'>
__class__ <class 'CLR.ModuleObject'> <class 'type'>
__file__ C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll <class 'str'>
python --version
Python 3.5.2
ipython --version
5.1.0
你能在兩種情況下顯示'__dict__'的內容嗎? – denfromufa