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:
...
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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 thedata
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:
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.