PyDev 内容辅助的一些令人不安的事情

发布于 2024-08-11 00:05:41 字数 760 浏览 3 评论 0原文

如下所示,

from UserDict import UserDict

class Person(UserDict):
def __init__(self,personName=None):
    UserDict.__init__(self)
    self["name"]=personName

在另一个模块中,我尝试实例化 Person 类的对象并打印其 docclass 属性:

import Person
p = Person.Person("me")
print p.__doc__
print p.__class__

我在 Python 中创建了一个简单的类, 当我在 Eclipse 中使用内容辅助时,docclass 不在实例化对象的属性列表中:

替代文本 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡写薰衣草的香 2024-08-18 00:05:41

编辑:

您的 Person 类是所谓的旧式类,因为它是旧式类 UserDict 类的子类。旧式和新式(即从object 派生的类)在特殊属性的可用性和处理方面存在根本区别。特别是,旧式类实例的 dir() 不会返回 __class__,而新式类的 dir()实例确实如此,并且毫无疑问,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__']

如最近的 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 from object) in the availability and treatment of special attributes. 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():

>>> 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. There are other disadvantages of using old-style classes and they have been removed entirely in Python 3, along with the UserDict module. You could get the benefits now, and get better info in PyDev, by changing the Person class to subclass directly from dict.

南七夏 2024-08-18 00:05:41

不确定 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文