Python Eclipse类型转换intellisense解决方案
假设我有以下两门课Python Eclipse类型转换intellisense解决方案,python,eclipse,intellisense,instance-variables,Python,Eclipse,Intellisense,Instance Variables,假设我有以下两门课 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() ite
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.
def do_something():
myTop=TopClass()
#创建两个项目
item1=ItemClass()
item1.name=“托尼”
item2=ItemClass()
item2.name=“迈克”
#将这些添加到顶级
myTop.items.append(item1)
myTop.items.append(item2)
#到目前为止,访问类成员与
#IDE(Eclipse)自动识别对象的类型
#并且可以解释正确的成员变量。-令人惊叹的!
#现在让我们试着做一个for循环
对于myTop.items中的myItem:
myItem.name#
我可能拼错了什么,直到我真正运行脚本才发现
目光短浅和虚伪
你可能因为没有进行单元测试而拼错了一些东西,直到你被起诉后才发现
“实际运行脚本”不是学习是否正确的时候
当您发现问题时,输入带有或不带有Eclipse intellisense的代码并不重要
当您发现问题时,不会运行脚本
单元测试就是当你发现问题的时候
请停止依赖Eclipse intellisense。请开始单元测试 问题1:您可以将参数传递给_init__
class ItemClass:
def __init__(self, name):
self.name = name
item1 = ItemClass("tony") # this is better
问题2:让编辑器为您工作,而不是为编辑器构建代码
myItemT = ItemClass() # this is misleading !!
# myItemT here is not same as above. What is some one changes this to x?
for myItemT in myTop.items:
.....
由于不同的错误,这可能会导致以后出现问题,并且编辑器不会在这方面帮助您
myItemT = ItemClass()
for myItemT in myTop.items:
do_something_with myItemT ...
# an indentation mistake
# This myItemT refers to the one outside for block
do_anotherthing_with myItemT ...
智能感知就是不知道你想让它知道什么。想想这段代码:
class Foo(object):
def __init__(self):
self.name = None
class Bar(object):
def __init__(self):
self.blub = None
bar1 = Bar()
bar2 = Bar()
bar1.blub = 'joe'
bar2.blub = 'jim'
items = [bar1, bar2]
each = Foo()
for each in items:
each.name = 'Wha?' # here Eclipse also filled in the name attribute,
# although each is never a Foo in this loop.
# And funny, this is perfectly valid Python.
# All items now have a name attribute, despite being Bars.
好吧,也许我想要这个的理由是错误的,如果我想要的只是让我的代码输入更有效,那该怎么办呢。Intellisense非常适合自动完成(尤其是长成员名)。假设我只想要这个,这样我就不必输入太多。另外,单元测试确实是我应该确定某些东西是否正常工作的地方,但是在这里自动完成至少不能“帮助”原因。我不认为这是一个破坏代码可靠性的特性。@Nick:在动态语言中,这是完全破坏性的。事实上,它有时绝对具有误导性,可能导致问题而不是解决问题。但是,当它工作时,您可以自由使用它。如果没有,你就没有失去任何东西。你只需要像其他人一样打字。