4

我正在制作一个 wxPython 应用程序,它为用户提供一个 shell。(这是wx.lib.shell.PyShellwxPython 附带的 shell。)

问题是,在这个 shell 中定义的.__module__属性不好。例如:

>>> def f(): 0
... 
>>> f.__module__
>>> f.__module__ is None
True
>>> class A(object):
...     pass
...     
>>> 
>>> A.__module__
'__builtin__'

我认为.__module__这两个对象的属性应该是__main__. 没有把握。但绝对不应该是Noneor __builtin__

如何让 shell.__module__为这些函数和类赋予良好的属性?

4

2 回答 2

1

In IDLE and in the wxPython Demo's PyShell demo, I get the following:

>>> def f(): 0

>>> f.__module__
'__main__'
>>> f.__module__ is None
False
>>> class A(object):
        pass

>>> A.__module__
'__main__'

It seems to work correctly to me. I'm not sure what you're doing on your machine. I am using Python 2.5, wxPython 2.8.10.1 on Windows XP.

于 2010-12-06T20:01:42.073 回答
0

发生这种情况的原因是因为我使用了自定义的本地字典,而忽略了在其中添加有意义'__name__'的内容。一旦你将一个'__name__'项目放入你提供给 PyShell 的 locals dict 中,它的值将用于设置.__module__shell 中定义的函数和类的属性。

于 2010-12-07T17:30:14.310 回答