LearnRevitAPI

Oct 25, 2024

Type Hinting

As you know, C# has a strict requirement for type declarations.

It can be annoying, but it's also very useful for your IDE to get correct autocomplete options and debugging.

Luckily, Python has a hidden feature for type-hinting. It can be really useful when you code and you know the types of elements.

Let me show you how to do that.

Type Hinting in IronPython

Python doesn't enforce declaring types like C#, but we can still include them to improve code readability and assist with debugging.

But keep in mind that Python2 and Python3 have slightly different rules for declaring types, so I will provide you with how to do Python2 type hinting because it also works in Python3.

Here is the deal:
You have to use #type: TypeName syntax at the end of the line.

Here is an example for the doc variable:

When you define doc, your IDE doesn't know its type and won't suggest you any autocomplete if you try to get any properties.

But if you define its type, you will get suggestions. That's why sometimes it can be a game changer, especially for beginners.

You can also define types in your loops and functions.

Type Hinting in Loops

Here is how to define types inside the for loop.

Type Hinting in Functions

Here is how to define types inside functions.

You put #type on the next line after def, and then you need to define types for all your arguments in the same order. Then, optionally, you can also define the type of the return value.

In my case, I provided that a bool will be returned.

Testimonial Highlight

Want to share your testimonial?
Click here

P.S.

If you want to Learn Revit API but you don't want to waste time - follow my step by step roadmap and be a part of the exclusive Revit API Community.

Check out ​Learn Revit API Course​.

You will get access to:

  • LearnRevitAPI Course

  • Exclusive Revit API Community

  • Upcoming WPF Course (in Nov)

  • Upcoming Python Course (TBA)

  • All Course Updates

Click Here to Join (Less than 30 seats left for Lifetime Access)

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

Last Chance To Claim LIFETIME ACCESS. [~20 Seats left]