As a programmer who has been using Unity to develop games for a long time, the programming language I use is C#. As you know, C# is a strongly typed language and is usually programmed using an object-oriented paradigm (of course, it has other paradigms). Recently, I started to use Python for programming. It has been 5–6 years since I last used Python, and at that time I used Python 2.7. Therefore, I had the impression that Python is a process-oriented dynamic programming language without types.

Of course, now I know I am wrong. And I found that the following practices can make programmers like me using C# also very accustomed to using Python.

Type Annotation & Type Comments

My first attempt was to make Python a strongly typed language, at least to make it look like a strongly typed language. The good news is, since Python3.5, Python introduced type annotation through PEP 484.

For example, here is a simple function whose argument and return type are declared in the annotations:

def greeting(name: str) -> str:
    return 'Hello ' + name

As you can see in the example above, type annotation is accomplished by adding : <type> after a variable. By the way, if the function does not return, the return type should be None.

def retry(url: Url, retry_count: int) -> None: ...

But what if the type is more complex? For example, the type is a list whose elements are int.

The typing module since Python 3.5 can be used for this purpose. This module provides runtime support for type hints as specified by PEP 484PEP 526PEP 544PEP 586PEP 589, and PEP 591. The most fundamental support consists of the types AnyUnionTupleCallableTypeVar, and Generic.

#programming-languages #programming #developer #coding #python

Make Python More like An Object-Oriented Programming Language
1.55 GEEK