简体   繁体   English

有关PyDev内容辅助的一些问题

[英]Something disturbing about PyDev content assist

I created a simple class in Python as follows, 我在Python中创建了一个简单的类,如下所示:

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: 在另一个模块中,我尝试实例化Person类的对象并打印其docclass属性:

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: 当我在Eclipse中使用内容辅助时,我以为docclass不在实例化对象的属性列表中,这让我感到困扰:

alt text http://img171.imageshack.us/img171/5169/pydevcontentassist.png 替代文字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. 在Java中,Eclipse显示了属性和方法的完整列表,这在我不想看Java Docs时有时对我有很大帮助。 I just figure things out using content assist. 我只是使用内容辅助解决问题。

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. 不确定PyDev开发团队以外的人是否真的可以在这里为您提供帮助,因为这基本上可以归结为功能问题/请求。

I'd suggest creating an item on their Feature Request tracker or their bug tracker . 我建议在其功能请求跟踪器错误跟踪器上创建一个项目。

EDIT: 编辑:

Your class Person is a so-called old-style class because it is subclassed from the UserDict class, an old-style class. 您的Person类是所谓的老式类,因为它是从UserDict类(一个老式类) UserDict而来的。 There are fundamental differences between old-style and new-style (ie classes that subclass from object ) in the availability and treatment of special attributes. 在特殊属性的可用性和处理方面,旧样式和新样式(即从object子类化的类)之间存在根本差异。 In particular, dir() of an instance of an old-style class does not return __class__ , whereas dir() of new-style class instances do, and, undoubtedly, PyDev is displaying the results of dir(): 特别是,旧式类的实例的dir()不会返回__class__ ,而新式类的实例的dir()返回__class__ ,并且无疑, PyDev将显示dir()的结果:

>>> class OldStyle: pass
... 
>>> os = OldStyle(); os.__class__; dir(os)
<class __main__.OldStyle at 0x100412cb0>
['__doc__', '__module__']
>>> class NewStyle(object): pass
... 
>>> ns = NewStyle(); ns.__class__; dir(ns)
<class '__main__.NewStyle'>
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

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 like dict . 如最近的Python Standard Library文档中所述,自从UserDict消失以来,由于在Python 2.2中引入了新型类,现在可以直接从诸如dict内置类型中UserDict子类。 There are other disadvantages of using old-style classes and they have been removed entirely in Python 3, along with the UserDict module. 使用旧式类还有其他缺点,它们与UserDict模块一起在Python 3中已被完全删除。 You could get the benefits now, and get better info in PyDev, by changing the Person class to subclass directly from dict . 通过将Person类直接从dict更改为子类,您现在可以获得收益,并在PyDev中获得更好的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM