在Eclipse + PyDev中实现函数参数的自动补全

3 投票
3 回答
1333 浏览
提问于 2025-04-16 00:38

我在Windows XP的电脑上使用Eclipse和PyDev,运行Iron Python。现在我有一个类的定义,这个类需要一个对象作为参数,而这个对象又是另一个类的实例,像这样:

myObject1 = MyClass1()
myObject2 = MyClass2(myObject1)

这两个类的定义在不同的模块里,一个叫myclass1.py,另一个叫myclass2.py。我希望在使用myObject1的时候,能够在myclass2.py文件中实现自动补全功能。换句话说,在myclass2.py文件里,我可能会写成这样:

""" myclass2.py """
class MyClass2():
    def __init__(self, myObject1):
        self.myObject1 = myObject1
        self.myObject1.  <============== would like auto code completion here

这样做有可能实现吗?

谢谢!

3 个回答

0

你的源文件夹里有一个 __init__.py 文件吗?这个文件可以是空的,但在所有文件夹里都应该有它,这样Python才能知道去读取里面的文件,以便进行自动补全。

1

在创建对象的时候,如果有一行垃圾代码(if False ...),在我的Pydev 2.5上是没问题的。

""" myclass2.py """
    class MyClass2():
    def __init__(self, myObject1):
        if False : myObject1 = MyClass1()
        self.myObject1 = myObject1        
        self.myObject1.  <============== would like auto code completion here
1

在使用Jython和PyDev/Eclipse的时候,我也有过这样的疑问。代码补全应该能识别你在MyClass2中用到的MyClass1的方法,但并不是整个API都能识别。我觉得这是因为你可以随时添加或删除类中的方法,所以Eclipse不能保证某个特定的方法一定存在,也不能保证方法列表是完整的。

举个例子:

>>> class a:
...     def b(self):
...         print('b')
...
>>> anA = a()
>>> anA.b()
b
>>> del a.b
>>> anA.b()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: a instance has no attribute 'b'

所以如果代码补全在这里显示了方法b(),那就是不对的。

类似的,

>>> class a:
...     pass
...
>>> anA = a()
>>> anA.b()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: a instance has no attribute 'b'
>>> def b(self):
...     print('b')
...
>>> a.b = b
>>> anA.b()
b

所以如果代码补全没有显示方法b(),那也是不对的。

我可能不是完全正确,但我觉得这个猜测还不错。:)

撰写回答