Pydev不会解析动态创建的属性

时间:2011-05-18 16:53:37

标签: python eclipse dynamic pydev resolve

问候SO,

我有一个奇怪的问题,我似乎无法解决:

我在Windows XP上使用eclipse Helios的pydev插件(如果重要的话)。

我有一个包含类的模块。此类的__init__采用一个参数来确定此类方法应具有的属性集。

由于我不允许显示实际代码,我可以给出以下类比:

class Car:
    def __init__(self, attrs):
        # attrs is a dictionary.
        # the keys of attrs are the names of attributes that this car should have
        # for example, a key of attr could be 'tires'

        # the values of attrs are the values of the attributes which are the keys
        # so if the key is 'tires', it's value might be 4

现在,因为我在运行时动态设置这些变量,所以当我这样做时,Pydev无法给出建议:

c = Car()
print c.tires

当我输入“c。” +,pydev不提供轮胎作为建议。

我如何才能获得此功能?或者它不是pydev目前可以做的事情吗?

我很感激任何帮助

2 个答案:

答案 0 :(得分:1)

这是所有动态语言IDE都会遇到的一般问题。 Pydev无法知道Car.__init__在Car实例上设置了哪些属性,而无需执行代码。如果对__init__中设置的属性使用类变量,Pydev应该能够提供自动完成建议。

class Car(object):
    tires = 4

    def __init__(self, attrs):
         self.tires = attrs.get('tires', self.tires)
         self.tires += attrs.get('spare', 0)

答案 1 :(得分:0)

+1给Imran他的解决方案。但是,我有更好的解决方案:

Create all attributes in `__init__`.
When it comes time to be dynamic, delete the unwanted attributes.

这样,虽然仍然在自动完成中建议所有属性的超集,但内存不会浪费。

相关问题