Python Book
🇺🇦 Stand with Ukraine🎓Training Suite
  • Book overview
  • Notes about this book
  • 1. Introduction to Python
    • What is Python
    • Basic syntax
    • Objects in Python
    • Python overview
    • Installation, IDEs etc.
    • ipython
    • Sources for self-learning
  • 2. Strings and numbers
    • Getting help
    • Introspection
    • Basic types
    • None object
    • Numbers
    • Strings
    • Unicode
    • String Formatting
    • Regular expressions
    • Sources for self-learning
  • 3. Containers
    • Data Structures
    • Lists
    • Tuples
    • Dictionaries
    • Sets
    • Conditions
    • Loops
    • Additional modules
    • Sources for self-learning
  • 4. Functions
    • Functions
    • Scopes of visibility
    • Generators
    • Lambdas
    • Type hints
    • Function internals
    • Sources for self-learning
  • 5. Functional Programming
    • Builtins
    • Iterable
    • Iterator
    • Functional Programming
    • Functools
    • Comprehensions
    • Additional modules
    • Sources for self-learning
  • 6. Code Styling
    • Zen of Python
    • Lint
    • PEP 8
    • Modules
    • Packages
    • Sources for self-learning
  • 7. OOP
    • OOP Basics
    • Code design principles
    • Classes
    • Method Resolution Order
    • Magic attributes and methods
    • Super
    • Sources for self-learning
  • 8. Decorators, Exceptions
    • Decorators
    • Exceptions
    • Sources for self-learning
  • 9. Testing
    • Basic Terminology
    • Testing theory
    • Dev unit testing vs QA automated testing
    • Best Practices
    • Doctest
    • Unittest
    • Test Runners
    • Pytest
    • Nose
    • Continuous Integration
  • 10. System Libs
    • Working with files
    • System libraries
    • Subprocess
    • Additional CLI libraries
Powered by GitBook
On this page

Was this helpful?

Edit on Git
  1. 4. Functions

Type hints

Some other languages such as Java/C++ (but not Python) have static typing. It allows to define all variables and return values of functions declaring which type of object those will be. The predefined types allow the compilers to check the code before compiling and running the program. Additionally IDE has better options to suggest available methods and linters - to check the correct way of working with objects.

In Python we can enable this be declaring types for each defined variable, function parameter and function return value.

Using type hints will give us these benefits:

  • Code will be much easier to read as it will be clear what types of object is supposed to be used.

  • IDE can suggest better auto completion results.

  • Linters/Type checkers like mypy can validate code against correctness and we can find out about problem before running without complex debugging.

Type hints are created using annotations - colon followed by a type we want to correlate with our parameter or return value.

Let's check the following function, it is quite hard to tell what types for which parameter this function expects:

def calc_square_ugly(x, y, u):
    return f"{x * y} {u}²" 

It would much more clear if we know what types to use. In the following example we see that x and y are int and u is of str type, additionally we now know that the function is going to return str:

🪄 Code:

def calc_square(x: int, y: int, u: str="m") -> str:
    return f"{x * y} {u}²" 

print(calc_square(5, 4))

📟 Output:

20 m²

If we complement example above with some docstring the type hints will be a bonus part of the help information about our new function:

🪄 Code:

def calc_square(x: int, y: int, u: str="m") -> str:
    """
    Calculate square of a rectangle with sizes x and y.
    Return a string with calculated value followed by units (meters) by default.
    """
    return f"{x * y} {u}²" 

help(calc_square)

📟 Output:

Help on function calc_square in module __main__:

calc_square(x: int, y: int, u: str = 'm') -> str
    Calculate square of a rectangle with sizes x and y.
    Return a string with calculated value followed by units (meters) by default.
PreviousLambdasNextFunction internals

Last updated 2 years ago

Was this helpful?