PyDev 内容辅助的一些令人不安的事情
如下所示,
from UserDict import UserDict
class Person(UserDict):
def __init__(self,personName=None):
UserDict.__init__(self)
self["name"]=personName
在另一个模块中,我尝试实例化 Person 类的对象并打印其 doc 和 class 属性:
import Person
p = Person.Person("me")
print p.__doc__
print p.__class__
我在 Python 中创建了一个简单的类, 当我在 Eclipse 中使用内容辅助时,doc 和 class 不在实例化对象的属性列表中:
替代文本 http://img171.imageshack.us/img171/5169/pydevcontentassist.png
为什么会发生这种情况?在 Java 中,Eclipse 显示了属性和方法的完整列表,有时当我不想查看 Java 文档时,这对我的开发很有帮助。我只是使用内容辅助来解决问题。
I created a simple class in Python as follows,
from UserDict import UserDict
class Person(UserDict):
def __init__(self,personName=None):
UserDict.__init__(self)
self["name"]=personName
In another module I try to instantiate an object of class Person and print its doc and class attributes:
import Person
p = Person.Person("me")
print p.__doc__
print p.__class__
It bothers me to think that doc and class are not in the list of attributes of an instantiated object when I use content assist in Eclipse:
alt text http://img171.imageshack.us/img171/5169/pydevcontentassist.png
Why does this happen? In Java, Eclipse shows the complete list of attributes and methods and this helps me a lot in development sometimes when I don't want to look at the Java Docs. I just figure things out using content assist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:
您的 Person 类是所谓的旧式类,因为它是旧式类 UserDict 类的子类。旧式和新式(即从
object
派生的类)在特殊属性的可用性和处理方面存在根本区别。特别是,旧式类实例的dir()
不会返回 __class__,而新式类的dir()
实例确实如此,并且毫无疑问,PyDev
正在显示 dir() 的结果:如最近的 Python 标准库文档,自从有了 Python 2.2 中引入新样式类,现在可以直接从内置类型(例如
dict.使用旧式类还有其他缺点,它们与 UserDict 模块一起在 Python 3 中被完全删除。您现在可以通过将
Person
类直接从dict
更改为子类来获得好处,并在 PyDev 中获得更好的信息。EDIT:
Your class Person is a so-called old-style class because it is subclassed from the
UserDict
class, an old-style class. There are fundamental differences between old-style and new-style (i.e. classes that subclass fromobject
) in the availability and treatment of special attributes. In particular,dir()
of an instance of an old-style class does not return__class__
, whereasdir()
of new-style class instances do, and, undoubtedly,PyDev
is displaying the results of dir():As described in recent Python Standard Library documentation, the need for
UserDict
has largely gone away since, with the introduction of new-style classes in Python 2.2, it is now possible to subclass directly from built-in types likedict
. There are other disadvantages of using old-style classes and they have been removed entirely in Python 3, along with theUserDict
module. You could get the benefits now, and get better info in PyDev, by changing thePerson
class to subclass directly fromdict
.不确定 PyDev 开发团队之外的任何人是否可以真正为您提供帮助,因为这基本上可以归结为功能问题/请求。
我建议在他们的 功能请求跟踪器 或他们的 < href="http://sourceforge.net/tracker/?group_id=85796&atid=577329" rel="nofollow noreferrer">错误跟踪器。
Not sure if anyone outside of the PyDev development team can really help you here, as this basically boils down to a feature question/request.
I'd suggest creating an item on their Feature Request tracker or their bug tracker.