开发者

Something disturbing about PyDev content assist

开发者 https://www.devze.com 2022-12-11 10:12 出处:网络
I created a simple class in Python as follows, from UserDict import UserDict class Person(UserDict): def __init__(self,personName=None):

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号