'does python interpreter reads what's inside function definition suite?

let's say I defined a function and never called it, will python interpreter ever read statements and expressions inside that function.

eg.

def foo(a, b):
       return a+b


Solution 1:[1]

Does this answer your question?


    $ python3
    Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
    [GCC 9.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def foo(a, b):
    ...        return a+b
    ... 
    >>> foo(1,2)
    3
    >>> foo(1,'2')
    Traceback (most recent call last):
      File "", line 1, in 
      File "", line 2, in foo
    TypeError: unsupported operand type(s) for +: 'int' and 'str'
    >>> foo('1','2')
    '12'
    >>> 

In words:

The first invocation: Parameters that are "compatible" will work fine.

The second invocation's first argument is a number, but not the second, so this causes a runtime error. Hardly any interpreted (non-type'd) programming language checks this prior to runtime.

Third invocation; compatible arguments again, both are strings, so concatenation happens.

e.g. C can check 'syntax' at compile time as you have to define variable and argument type beforehand.

Note that python will interpret statements in a file that is loaded if you have them outside def's;
i.e. import will execute a print(...) at "compile time".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1