DEV Community

Discussion on: 4 Useful Things in Python

Collapse
 
r0f1 profile image
Florian Rohrer

Hi,
Thanks for your comment, I think you are right. If you pass a string to foo() you also get some unexpected behavior. You already posted a remedy to the situation: checking the type of the data argument. That made me think, if there are other ways, that do not rely on type checking. (I know, type checking is not an expensive operation, but still...)

Two more ways, you could alleviate the situation: Working with conventions, if the caller does not respect the contract, they get unexpected behavior:

def foo(alist):
    """This function expects a list."""
    ...

Using type hinting: Since Python 3.5 you can specify the type of arguments, and static type checkers can use this information, to determine if you pass the correct types.

def foo(data: list) -> None:
    ...