Basics

Static Typing

  • Only used for documentation, it doesn't change anything in the code.

  • Can be used with mypy  to perform type checking during code compilation, but it's annoying because it still provides no real safety/checks while writing code.

  • Type Hints: Explanation .

  • Generic Types (T) for functions in Python 3.12 .

    • In Python 3.12 you can use 'any' type for generic functions.

  • Cython .

    • "Cython is a superset of Python syntax - almost any valid Python code is also valid Cython code. The Cython compiler translates the quasi-Python source code to not-for-human-eyes C, which can then be compiled into a shared object and loaded as a Python module."

    • "You can basically take your Python code and add as many or as few static type declarations as you like. Wherever types are undeclared, Cython will add in the necessary boilerplate to correctly infer them, at the cost of worse runtime performance. This essentially allows you to choose a point in the continuum between totally dynamically typed Python code and totally statically typed C code, depending on how much runtime performance you need and how much time you are prepared to spend optimizing. It also allows you to call C functions directly, making it a very convenient way to write Python bindings for external libraries."

  • Tips to Improve Function Syntax* .

    • Tip 4 shows the use of an 'asterisk' in functions, indicating that the type should be passed during the function call.

Type Check

if type(lista[0]) == str:
    print('lista[0] is a string')

Debug

  • You can use pass  or ...  to 'pass' a function.

  • Comments are done with #  or """ multi-line """ .

Formatting

  • Convert all floats to .2f and change to string.

    • a = str('%.2f' % a)

Ternary

  • [i * 10 for i in eixo_y0] .