说我有以下两个类。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.
任何意见以上?有没有更好的方式来完成这项工作?
好吧,也许我想要这样的理由是错误的,如果我正在寻找的是让我的代码输入更有效。智能感知非常适合自动完成(特别是长会员名称)。所以我们说我只是想要这个,所以我不必输入太多。 – Nick 2010-11-05 23:56:26
另外,单元测试确实是我应该确定某个东西是否正常工作的地方,但是至少不会在这里自动完成“帮助”原因。我不认为这是编码可靠性的破坏性特征。 – Nick 2010-11-06 00:01:27
@Nick:这对动态语言完全有害。事实上,这有时会造成误导,并可能导致问题无法解决。但是,您可以在工作时自由使用它。当它不,你没有**丢失任何东西。你只需要像其他人一样打字。 – 2010-11-06 01:34:27