Skip to content

Programming for Scripts

Extensive resources relating to Python can be found at This section is intended as a very brief introduction to the language to help a user to read and understand this manual section.


Python is a scripting language. Scripts are interpreted by the Python engine directly, without the need for compilation and linking steps common to compiled languages such as C or Java, so editing and execution can be carried out directly within Aimsun Next. Python’s other advantages over C++ include the fact there is no need to worry about pointers and memory management.

One feature of Python which can cause problems, particularly to programmers from other less strict languages, is the importance of indentation to program structure. Where other languages use braces, {}, Python relies on strict indentation to mark out sections of code, ends of loops, etc. On the plus side, this does in theory at least make the code more readable, particularly between authors.


Anything to the right of a hash (sometimes called pound sign) is a comment, and will not be interpreted on execution.

# This is a comment. Python will not attempt to interpret this
# print "Hello World"
# nothing happens, the print statement is behind a comment

There is no need to declare a variable: it is created assigning a value. Moreover, Python is a dynamically typed language, so different data types can be assigned to the same variable.

myInteger = 42 # an integer
myString = "Hello World" # a string
myPi = 3.142 # a float
myEmpty = None # a null
myInteger = myPi # now myInteger is a float

Python contains the following basic data structures:

  • Numbers, including integers and floating point.
  • Strings, enclosed by " " or ' ', and including special characters e.g. \n to start a newline.
  • Boolean, True or False.
  • Lists e.g. myList = [item1, item2, item 3] where items are accessed by index in the range 0 to (n-1), eg myList[1] would return item2.
  • Dictionaries, in which values are stored with a key, and accessed by the key which maps to the value.

For example, the following command:

myAddresses = {'grail': 'Castle Aargh', 'arthur': 'Camelot', 'knight ni': 'shrubbery'}

would create a map as follows

Key Value
grail Castle Aargh
arthur Camelot
knight ni shrubbery

While myAddresses['arthur'] would return 'Camelot'

Strings can be treated as lists of characters. The main operations which will work with any list but are most useful for strings are:

  • myString = myString1 + myString2 : Creates myString as a concatenation of two strings.
  • myString[i] : Returns character at index i.
  • myString[i,j] : Returns a substring from index i to j-1.
  • len(myString) :Returns the length of myString.

Object Oriented concepts

This is a general, and by no means complete, introduction to object oriented concepts to help a user read and understand this manual section.


A class is a blueprint, or prototype, that defines variables and methods common to all objects of a certain kind. Objects are concrete instances of a class, a bundle of related methods and attributes.

Examples of classes and potential objects are given below:

Class Objects
Vehicle type car, lgv, hgv, bus, coach
Car type focus, golf, mini
Car chittyChittyBangBang, knight 2000, M_33EJB

Note the convention for naming classes with the first letter in upper case and objects with a first letter in lower case.

What constitutes a class and what constitutes an object can be application specific.The table above shows how a class scheme can be used to describe vehicle types, sub types of a vehicle type and finally specific vehicles.


Attributes are values that define the object. Attributes are common to all objects of the same class, although their values are different

Class Attributes
Vehicle route, start_time
Vehicle type travel_mode, pcus
PrivateCarType manufacturer, model, weight, power
IndividualCar owner, passengers, registration_number, color, age

The attributes of an object, if publicly accessible, can be accessed by the convention object.attribute, e.g. to obtain the age of a specific car, we would use M_33EJB.age, or to find the weight of a Mini we would use mini.weight.


A method is a function the object is capable of performing. Objects of the same class share the same class methods. Methods can take several parameters as inputs and often return a value. Methods can be used to:

  • Get a value from an object: myCar.getNumberOfWheels().
  • Set a value for an object: myCar.setMaximumPassengers(6).
  • Make the object carry out some operation or calculation:
    • myCar.accelerate(rateOfAcceleration, TargetSpeed).
    • myCar.calculateMaximumSpeed().
    • myCar.addPassengerToPassengerList("Alma Cogan").

Methods accessible to clients (other objects, or you as the programmer) are public methods. Class attributes, along with some internal methods, are often kept private, and can only be accessed indirectly, through a well defined set of public methods which form the interface for the class. These public methods might enforce some constraint to protect the object’s data from corruption and inconsistencies.


Inheritance allows new classes to inherit state and behavior from existing classes, where the new class is a logical subtype of its parent class. From its parent it inherits methods which it can override/reimplement, and also attributes.

For example, a programmer could define separate unrelated classes to describe cars, trucks, and buses. However, these vehicle classes share common methods and attributes and, for many aspects in the program, would be treated the same way. Therefore they could be modeled with the same class and simply contain an attribute stating whether they are a “Car”, “Truck” or “Bus”, but if we want to offer specific methods to for example buses such as pickUpPassenger(busStopNumber) then this would not be adequate.

Inheritance solves this problem by allowing a generic parent class, Vehicle, to be specified, defining all the common methods (eg accelerate(parameters), brake(parameters)), and attributes (maxPower, length, weight). These can then be overridden in the child classes so the vehicle type (“Car”, “Truck” and “Bus”. ) could override methods or attributes and provide more appropriate functions or values, similarly a specific vehicle type ( i.e. the mini) could specialize them further. They can also be extended in child classes to add class specific methods and attributes. A "bus" vehicle type class could extend the Vehicle class by adding methods and attributes for scheduled departure, timetable, and bus stop dwells, which would be irrelevant to other vehicle types.