Python Eclipse 类型转换智能感知解决方法
假设我有以下两节课。
class TopClass:
def __init__(self):
self.items = []
class ItemClass:
def __init__(self):
self.name = None
我想按以下方式使用它:
def do_something():
myTop = TopClass()
# create two items
item1 = ItemClass()
item1.name = "Tony"
item2 = ItemClass()
item2.name = "Mike"
# add these to top class
myTop.items.append(item1)
myTop.items.append(item2)
# up until this point, access class members is effortless as the
# IDE (Eclipse) automatically recognizes the type of the object
# and can interpret the correct member variables. -- Awesome!
# now let's try and do a for loop
for myItem in myTop.items:
myItem.name # <- I HAD TO TYPE the ".name" IN MANUALLY,
# THIS IS ANNOYING, I could have misspelled
# something and not found out until
# I actually ran the script.
# Hacky way of making this easier
myItemT = ItemClass()
for myItemT in myTop.items:
myItemT.name = "bob" # <- Woah, it automatically filled in the
# ".name" part. This is nice, but I have the
# dummy line just above that is serving absolutely
# no purpose other than giving the
# Eclipse intellisense input.
对上述内容有什么意见吗?有没有更好的方法来完成这项工作?
Say I have the following two classes.
class TopClass:
def __init__(self):
self.items = []
class ItemClass:
def __init__(self):
self.name = None
And I want to use that in the following way:
def do_something():
myTop = TopClass()
# create two items
item1 = ItemClass()
item1.name = "Tony"
item2 = ItemClass()
item2.name = "Mike"
# add these to top class
myTop.items.append(item1)
myTop.items.append(item2)
# up until this point, access class members is effortless as the
# IDE (Eclipse) automatically recognizes the type of the object
# and can interpret the correct member variables. -- Awesome!
# now let's try and do a for loop
for myItem in myTop.items:
myItem.name # <- I HAD TO TYPE the ".name" IN MANUALLY,
# THIS IS ANNOYING, I could have misspelled
# something and not found out until
# I actually ran the script.
# Hacky way of making this easier
myItemT = ItemClass()
for myItemT in myTop.items:
myItemT.name = "bob" # <- Woah, it automatically filled in the
# ".name" part. This is nice, but I have the
# dummy line just above that is serving absolutely
# no purpose other than giving the
# Eclipse intellisense input.
Any opinions on the above? Is there a better way of making this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
短视和虚假。
您可能拼错了某些内容,但直到您因为没有进行单元测试而遭受诉讼时才发现。
“实际运行脚本”并不是您了解自己是否做得正确的时间。
使用或不使用 Eclipse 智能感知键入代码都不是您发现问题的时候。
运行脚本并不是当你发现问题的时候。
单元测试是发现问题的时候。
请停止依赖 Eclipse 智能感知。请开始单元测试。
Short-sighted and false.
You could have misspelled something and never found out until you endured a lawsuit because you did no unit testing.
"actually ran the script" is not the time when you learn if you did it right.
Typing code with or without Eclipse intellisense is not when you find the problems.
Running the script is not when you find the problems.
Unit testing is when you find the problems.
Please stop relying on Eclipse intellisense. Please start unit testing.
IntelliSense 只是无法知道您想让它知道什么。想想这段代码:
IntelliSense just can't know what you want it to know. Think of this code:
问题 1:您可以将参数传递给 __init__
问题 2:让编辑器为您工作,而不是为编辑器构建代码。
由于不同的错误,这可能会在以后引起问题,并且编辑器不会为您提供帮助。
Issue 1: You could pass arguments to __init__
Issue 2: Make editor work for you and not structure your code for editor.
This can cause an issue later on due to different mistake and editor will not help you there.