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 7400+ people!