Python is a dynamically typed, interpreted programming language, unlike statically typed, compiled languages like C++. But it can be!
You can write Python code in a statically typed way using type hints, check for type errors using mypy, and you can compile any Python using Nuitka.
So, if you mix them both, you get statically typed, compiled Python code!
Let's see how we can achieve this.
-
Install mypy.
pip install mypy
-
Install Nuitka.
pip install nuitka
(You may need to install python3-devel first)
That's it. Now we can start writing our code.
Example
Let's define a Person class, that has a name that is a string, an age that is an int, a job that is a string, and a salary that is a float.
The code would look like this. Create a file called main.py (or any name you want).
class Person:
name: str
age: int
job: str
salary: float
def __init__(self, name: str, age: int, job: str, salary: float) -> None:
self.name = name
self.age = age
self.job = job
self.salary = salary
def increase_salary(self, amount: float) -> None:
self.salary += amount
def __str__(self) -> str:
return f"Hello, my name is {self.name}, I am {self.age} years old, and I am a {self.job}."
adham: Person = Person(name="Adham", age=23, job="Engineer", salary=10000)
amount: float = 5000
adham.increase_salary(amount)
print(adham)
Run "python main.py" in the terminal.
We have used type hints to specify the types, but Python doesn't enforce these types, you can give a str a value of an int and Python wouldn't throw any errors.
For example, change the value of the variable amount to a string.
Here my code editor gave me a hint that the type is incompatible, but Python will run the code, it will only throw an error at runtime when it executes the icrease_salary method.
So how to catch these type errors without executing the code?
This is where mypy comes into play.
Run "mypy main.py" in the terminal.
You can see that mypy caught the error.
If we change the value of amount back to 5000 and run mypy again, here is the output.
Now that we are sure that there aren't any type errors in our code, we can finally compile out code using Nuitka.
Run "nuitka3 main.py" in the terminal.
Run "./main.bin" in the terminal to run the compiled code.
A slightly easier way to type check and compile our code in the same command is running "mypy main.py && nuitka3 main.py".
This will type check our code first, and if it's correct, it will get compiled.
That's it! Now you have Python++!
Top comments (0)