说我有以下两节课。
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.
对以上有什么意见吗?有没有更好的方法可以使这项工作?
最佳答案
我可能拼错了一些内容,直到真正运行脚本后才发现。
近视和虚假。
您可能拼错了一些内容,直到忍受了诉讼才发现,因为您没有进行单元测试。
“实际运行脚本”不是您了解自己是否做对的时间。
当您发现问题时,无论是否使用Eclipse intellisense都不会键入代码。
发现问题后,便不会运行脚本。
当发现问题时进行单元测试。
请停止依赖Eclipse intellisense。请开始单元测试。