Python: Definitions made in external shell get a bad `.__module__` attribute
I am making a wxPython app which supplies a shell for the user to use. (This is wx.lib.shell.PyShell
, the shell that ships with wxPython.)
Problem is, definitions made in this shell have a bad .__module__
attribute. For example:
>>> def f(): 0
...
>>> f.__module__
>>> f.__module__ is None
True
>>> class A(object):
... pass
...
>>>
>>> A.__module__
'__builtin__'
I think the .__module__
attribute开发者_C百科 for both these objects should be __main__
. Not sure. But it definitely shouldn't be either None
or __builtin__
.
How can I make the shell give a good .__module__
attribute to these functions and classes?
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.
The reason this happened is because I was using a custom locals dict, and neglected to put a meaningful '__name__'
in it. Once you put a '__name__'
item in the locals dict you give to PyShell, its value will be used to set the .__module__
attribute for functions and classes defined in the shell.
精彩评论