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.