python 中缺乏自动完成/转换的问题
我遇到一种情况,在第一类中我声明数组,然后将其传递给另一个对象,该对象打印该数组中元素的名称。它有效,但是当我输入“汽车”时。在 ReadCarNames ide 中不建议我“名称”?我正在 wing ide 4 pro 中尝试。我可以在方法 ReadCarNames 中投射汽车吗?
########################################################################
class MyClass:
""""""
#----------------------------------------------------------------------
def __init__(self):
cars=[]
cars.append(Car('bmw'))
cars.append(Car('audi'))
reader=Reader()
reader.ReadCarNames(cars)
########################################################################
class Car:
""""""
#----------------------------------------------------------------------
def __init__(self,name):
self.name=name
########################################################################
class Reader:
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
def ReadCarNames(self,cars):
for counter,car in enumerate(cars):
print str(counter) +' '+ car.name
I have a situation where in first class I declare array, and I pass it to another object, which prints name of elements in this array. It works, but when I input 'car.' in ReadCarNames ide doesn't suggest me 'name' ? I'm trying it in wing ide 4 pro. Can I cast car in method ReadCarNames ?
########################################################################
class MyClass:
""""""
#----------------------------------------------------------------------
def __init__(self):
cars=[]
cars.append(Car('bmw'))
cars.append(Car('audi'))
reader=Reader()
reader.ReadCarNames(cars)
########################################################################
class Car:
""""""
#----------------------------------------------------------------------
def __init__(self,name):
self.name=name
########################################################################
class Reader:
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
def ReadCarNames(self,cars):
for counter,car in enumerate(cars):
print str(counter) +' '+ car.name
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
参见这里:
http://www.wingware.com/doc/edit/helping-wing -analyze-code
您的 IDE (Wing) 不确定
汽车
中的对象是什么类型,但您可以告诉它什么car
带有一个断言语句,它将按照您想要的方式进行自动补全。如果你愿意的话,你可以将其视为为 Wing 的眼睛铸造类型。或者,如果您不希望该断言始终触发,您可以将其包装在 Wing 的 SourceAssistant 拾取的“if 0”逻辑中,但 python 不会执行。
您目前无法告诉 Wing 列表/元组/等仅包含一种类型的对象及其含义,但它在他们的计划中并且将使用类似的语法。
See here:
http://www.wingware.com/doc/edit/helping-wing-analyze-code
Your IDE (Wing) doesn't know for sure what type of objects are in
cars
, but you can tell it whatcar
is with an assert statement and it will do autocompletion exactly how you want it to. You can view it as casting the type for Wing's eyes only if you like.or if you don't want that assert firing off all the time, you can wrap it in 'if 0' logic that Wing's SourceAssistant picks up but python will not execute.
You currently cannot tell Wing that a list/tuple/etc only contains one type of object and what it is, but it is in their plans and will use similar syntax.
在 Wing IDE 中工作的一个好方法是设置一个断点,运行到它,然后您将在编辑器(在活动调试堆栈上的代码中)和调试探针中获得运行时源分析。这在 http://wingware.com 上倒数第二个“静态和运行时分析”屏幕截图中进行了说明/wingide/代码智能
A good way to work in Wing IDE is to set a breakpoint, run to it, and then you'll get runtime-sourced analysis in the editor (in code that's on the active debug stack) and Debug Probe. This is illustrated in the "Static and Runtime Analysis" screen cast, second from last on http://wingware.com/wingide/code-intelligence
IDE 不知道从 enumerate 返回的类型,因此在这种情况下无法执行自动完成。它也不知道
cars
列表包含Car
。The IDE does not know the type that is being returned from enumerate, and therefore cannot perform autocomplete in that situation. It also does not know that the
cars
list containsCar
.由于 Python 的动态特性,如果不运行代码,就不可能知道实例是什么类型,甚至无法知道它具有哪些属性。例如,您的
Car
实例在实例化之前没有name
属性,因此即使 IDE 以某种方式知道car
是以Car
为例,它需要花很长时间才能弄清楚它静态具有哪些属性。这取决于您的 IDE,但某些 IDE(例如 Python 附带的 IDLE)在您运行脚本后会给出更好的结果。但在这种情况下,可能不会。
Due to the dynamic nature of Python, it's impossible to know what type an instance is, or even what attributes it has, without running the code. For example, your
Car
instances don't have aname
attribute until they are instantiated, so even if the IDE somehow knew thatcar
was aCar
instance, it would have a devil of a time figuring out what attributes it would have statically.It depends on your IDE, but some IDEs (such as the IDLE that comes with Python) will give better results after you run your script. In this case, though, probably not.