Python ProgrammingPython Programming

How do I pass instance of an object as an argument in a function?

class Vehicle:
    def __init__(self):
        self.trucks = []

    def add_truck(self, truck):
        self.trucks.append(truck)


class Truck:
    def __init__(self, color):
        self.color = color

    def __repr__(self):
        return "{}".format(self.color)


def main():
    v = Vehicle()
    for t in 'Red Blue Black'.split():
        t = Truck(t)
        v.add_truck(t)
    print(v.trucks)


if __name__ == "__main__":
    main()

In the example there are two classes Vehicle and Truck, object of class Truck is passed as parameter to the method of class Vehicle. In method main() object of Vehicle is created. Then the add_truck() method of class Vehicle is called and object of Truck class is passed as parameter.

Sample output of above program.
[Red, Blue, Black]