Kickstart Python For Revit API (3)

Promise, this is the last post about Python abstract concepts and basics. Also, this is the most important lesson, it will explain the tools that will allow you to write useful code and apply it to Dynamo.

The flow of execution

In Python, the order of execution follows a linear pattern; it runs the first line of code, then the second and so on. However, there is a way to override this behavior, and the flow of execution will not be longer linear, you can just skip what you don´t want to be executed at that moment or leave that chunk of code for later. Here is a diagram that explains how this works:

This image has an empty alt attribute; its file name is flow-chart.png


The first instrument to alter the linear flow of execution are the conditionals. As shown in the previous diagram, conditionals execute the block of code inside the condition if the required condition is met. For example:

if (raining == True):
elif (snowing == True):

A consideration to take into account, if the first requirement is met, the rest of the conditions will be skipped even if they are true. If the variables raining and snowing are both true, only the block inside if is run. So the order in which they are written is important if we are writing more than one condition.


Besides those three conditional statements (if, elif, else) to control execution flows, there are also loops:

for statements allows general mapping operations over a series of elements. For example, suppose that we want to number all rooms sequentially:

i = 0
roomNumbers = list()
for room in roomsList:
i = i + 1

while statements allow us to keep performing the same operation until a condition is satisfied. Eg:

while i < roomsList.length():
i += 1

Although in this example the while loop does the same than the for loop, and it is more prone to error the while loop, it can come handy sometimes.

Leave a Reply

Your email address will not be published. Required fields are marked *