Pydev 不会解析动态创建的属性
你好,
我有一个奇怪的问题,我似乎无法解决:
我正在 Windows XP 上使用 pydev 插件和 eclipse Helios(如果有的话)。
我有一个包含类的模块。此类的 __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 目前无法做到的事情?
我将不胜感激任何帮助
Greetings SO,
I have a weird problem that I don't seem to be able to solve:
I am working with the pydev plugin with eclipse Helios on Windows XP (if it matters).
I have a module which contains a class. The __init__
of this class takes a parameter which determines the set of attributes that a method of this class should have.
Since I am not allowed to show actual code, I can give the following analogy:
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
Now, since I'm setting these variables dynamically at runtime, Pydev is not able to give me suggestions when I do this:
c = Car()
print c.tires
When I type in "c." +, pydev does not offer tires as a suggestion.
How might I go about getting this functionality? Or is it just not something that pydev can do at present?
I'd appreciate any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是所有动态语言 IDE 都会遇到的普遍问题。如果不执行您的代码,Pydev 无法知道 Car.__init__ 为 Car 实例设置了哪些属性。如果您使用类变量作为您在 __init__ 中设置的属性,Pydev 应该能够提供自动完成建议。
This is a general problem which all dynamic language IDEs suffer. Pydev has no way of knowing what attributes
Car.__init__
sets on instances of Car, without executing your code. If you use class variables for the attributes you set in__init__
, Pydev should be able to offer autocomplete suggstion.为 Imran 的解决方案+1。但是,我想到了一个更好的解决方案:
这样,虽然在自动完成中仍然建议所有属性的超集,但内存并没有浪费。
+1 to Imran for his solution. But, Ia better solution occurred to me:
This way, ene though the superset of all attributes is still suggested in the autocomplete, memory is not wasted.