Why doesn't python allow me to set a function variable to an integer?

Here I keep getting an error message next to int(age) in my function definition. It says the syntax at that position is invalid but I’ve used the same syntax for the children variable

medical_records = {}

def updated_medical_records(name, int(age), sex, float(bmi), int(children), smoker, float(insurance)):

    medical_data = {"Age":age, "Sex":sex, "BMI":bmi, "Children":children, "Smoker":smoker, "Insurance_cost":insurance}
    record = {name: medical_data}
    return medical_records

name = "Joseph"
age = 27.3
sex = "Male"
bmi = 17.3
children = 3
smoker = "Smoker"
insurance = 1000000

updated_medical_records(name, age, sex, bmi, children, smoker, insurance)

Due to the way the Python interpreter works, it won’t care about what you’ve done for the children parameter - it’ll never reach it because it throws the syntax error before it gets there, for your use of int(age) as a parameter.

Python, whilst a typed language, does not enforce type on variables or parameters - this is why when you define a string variable, for example, you simply type a_string = 'some text' and that’s sufficient.

You can provide type hints in your code, though. Whilst these are not enforced, and do nothing to prevent someone providing a different type to the one specified, they offer a suggestion as to the intended use. (If you’re working in VSCode, for example, when you make a call to a particular function it will use the type hints to show the expected output type as well as the expected type of the parameters.)

This is how you would use type hints:

def patient_search(name: str, max_results: int = 10) -> dict:
   # some function code here

Here we are advising that the name parameter ought to be a string, max_results ought to be an integer (with a default value of 10, if the call to the function doesn’t specify otherwise), and that the function will return a dictionary type. None of this is enforced, of course, so your code would still be expected to validate the input - checking that what you expect to be a string/integer is not actually a boolean and a set - but you have more information about the code than you might if all you had was the parameter names.


Notwithstanding the excellent information given above by @thepitycoder, we will have discovered that on the basis that parameters are really just positional placeholders/labels for argument objects, they cannot be computed values. That means we cannot make function calls from within the parameter list. Those calls would be either in the arguments list (at the caller) or before the call.