LearnRevitAPI

Jan 22, 2023

Translate C# to Python

๐Ÿง‘โ€๐Ÿ’ป As a Revit API developer, it's important to understand C# syntax. Many forums and Revit API documentation have plenty of C# examples, so it can open up a lot of new learning resources.

๐Ÿ’กAnd don't worry, you don't need to learn everything about C#. You just need to understand main differences so you can turn C# code into python.

Type Hinting

One common difference between C# and python is Type Hinting.

๐Ÿ“ฆ Type hinting is a way for a programmer to give a label to a variable so a computer knows what kind of data it should hold. It will allow it to catch mistakes earlier and gives you better suggestions. 

๐Ÿท๏ธ While C# requires you to give a label to a variable, it's not required in python. But it's still possible to do. So I am going to leave type hinting in examples for python in case you wonder how to do it.

C# vs Python

๐Ÿ” There are many differences between C# and python. Especially built-in features. However, most of the concepts and patterns can be translated between each other with a little bit of effort.

๐Ÿ“‘ And since we are dealing with Revit API, all the classes, properties and methods are the same. So my goal is to show you that it's not so complicated, and you can take any C# snippet about Revit API, and convert it into python.

Syntax Comparison

Code Blocks

Let's dive straight into comparison.

๐Ÿง  Keep in mind that C# uses curly braces {} to define code blocks and semi colon ; to finish statements, whereas python just uses indentation with spaces or tabs to define logical code blocks. 

โžก๏ธ For example whenever you create a function in python; all related code will be shifted to the right to define a code block. In C# curly brackets will be used to define scope of the function. 

๐Ÿ‘‡ Check Example below.

๐Ÿ’ก This example contains a lot, so let's start from the beginning.

Variables

#๏ธโƒฃ Whenever we define a variable in C# we also need to specify type of a variable, or use a 'var' keyword for undefined types.

๐Ÿ In python we just assign data to a variable. There is a syntax to specify type of a variable, but it's not necessary and thus not as widely used. 

๐Ÿ’ก To add Type Hinting to a variable in python we need to write ' # type: ' in the end of the line and specify data type (int, str, list or user defined type...)

In C# we specify Type before the variable name or we can use keyword 'var' to create a variable without defined type. And each statement is closed with semicolon.

Here is an example for creating an instance of FilteredElementCollector with Revit API.  

๐Ÿ’ก Python is clear. But in C#:
- We assigned type of FilteredElementCollector before variable name
- We use 'new' keyword when we use constructors
- The line ends with a semi colon ; ๐Ÿคฎ

If you remove all of these extra things that C# requries you will get a python syntax.

Conditional Statements

Let's compare conditional statements. Notice that print equvalent in C# is Console.WriteLine() method.

๐Ÿ’กC# uses curly brackets {} to define scope of if statements as well. And pythonic elif is written as else if in C#

Loops

โžฟ Loops are very similar between C# in python. However, creating a range of numbers will look very foreign if you see it for the first time. 

I won't go into explaining range, but you can compare it yourself in examples  below.

Function

โน๏ธ Let's compare functions. There are 2 functions in screenshots. The first one does not return anything and second one has arguments and it returns some data.

#๏ธโƒฃ In C# we have to specify data type of the returned value from a function before function name. If function does not return anything, then a keyword 'void' is used to indicate that. Also every argument in function has to include type hinting in C#. 

๐Ÿ The same type hinting can be achieved in python too and it can be very beneficial for making robust and readable code. I would definitely recommend doing it for reusable functions, but as a beginner you might not need to worry too much about it

๐Ÿ’ก There are multiple ways to define type hinting in python especially in newer version of python. I prefer to use the method that works both in python2 and python3.

You need to create special a comment under your function name and specify types in the same order as your arguments. 
#type: (arg1, arg2) -> return_type 

Class & Methods

NB! This email is longer than my usual one, and gmail seems to clip it around this point.

There is a message below '[Message clipped] View entire message'. You can view the whole thing by opening the link in that message. 

๐Ÿ’ก This class inherits everything from ISelectionFilter and you can see a little difference in syntax.

Methods are declared similar to functions. but it also specifies access level in the beginning.

In short, C# uses keywords like public, private and static to control who can access and use the methods, whereas Python uses a convention underscore _ prefix to indicate that the method is intended to be used internally by the class.

Conclusion

๐Ÿ“ So these are the basics that you need to understand in C# to translate it to python. There will be more complicated examples, where it won't be as simple to translate, but it doesn't mean it's impossible. 

I think it will come with practice. I managed to translate C# code to python without even knowing about syntax differences as I have just explained to you. 

I sort of tried to recognize patterns and write similar thing in python and sometimes it worked, sometimes it didn't. But over time I got better at it, and I understood more about C#.

Translate Revit API Docs

๐Ÿ”Ž Lastly, let's pick some example from Revit API Documentation and turn it into python. I will go with simple Wall.Create() method

Wall.Create( )

๐Ÿ‘‰ Here is a link to this method in Revit API documentation. I won't go into details, but you can compare 2 snippets below and make you own conclusions.
*Python type hinting is optional.


Closing Notes

๐Ÿคฏ You might start to think that C# is stupid because of all the curly brackets and some other stuff. But in reality it's just a different languages and they have different use cases. 

Both are great languages, each with its own strengths and weaknesses, and both are widely used by developers. 

๐Ÿ‘‡๐Ÿ˜„ But you might feel this way when you start to deal with C# after python.

P.S. ๐Ÿ™‹โ€โ™‚๏ธ If you are a C# developer, let me know if I should have explained anything else, or maybe you disagree with something. I would love to hear some feedback.

Enjoyed Newsletter?

Leave a Testimonial Here.

โŒจ๏ธ Happy Coding!
Erik Frits

Join Newsletter

๐Ÿ“ฉ You will be added to Revit API Newsletter

Join Us!

which is already read by 6800+ people!

Last Chance To Claim LIFETIME ACCESS. (Only ~50 Seats Left)