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
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 what car
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.
class Reader:
def __init__(self):
"""Constructor"""
def ReadCarNames(self,cars):
for counter,car in enumerate(cars):
assert isinstance(car, Car) # this trains Wing
print str(counter) +' '+ car.name # autocompletion will work here
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.
if 0: assert isinstance(car, Car)
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.
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
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 contains Car
.
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 a name
attribute until they are instantiated, so even if the IDE somehow knew that car
was a Car
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.