【问题标题】:Pydev won't Resolve Dynamically created AttributesPydev 不会解析动态创建的属性
【发布时间】:2011-08-28 05:48:03
【问题描述】:

你好,

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

我正在 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目前做不到?

感谢您的帮助

【问题讨论】:

  • 一般来说没有人可以解析动态创建的东西

标签: python eclipse dynamic pydev resolve


【解决方案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),但感觉很老套。我想知道是否有更好的方法。
【解决方案2】:

+1 给伊姆兰的解决方案。但是,我想到了一个更好的解决方案:

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

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

【讨论】:

    猜你喜欢
    • 2020-04-12
    • 2013-04-07
    • 2018-12-10
    • 1970-01-01
    • 2017-09-17
    • 2019-05-02
    • 2020-01-08
    • 1970-01-01
    相关资源
    最近更新 更多