Working with Strings, Integers, and Floats
Functions can accept different data types as arguments if the function's operations are compatible with those types.
One Function, Several Value Types
A function does not always require one fixed data type. Its compatibility depends on what the function does with its argument. The function print_twice demonstrates this idea: it prints its argument two times, so it can work with a string, an integer, or a floating-point number as long as the value can be printed.
The operation performed by the function determines which argument types are suitable. The function does not need to ask whether its argument is a string or a number before printing it.
Tracing Each Call
What do you think happens?
What happens when print_twice receives a string, then an integer, then a floating-point number?
Reveal answer
Answer: All three calls print their argument twice
The function performs the same operation in every call: it prints the value bound to arg, then prints arg again. Strings, integers, and floating-point numbers can all be printed.
Binding Before Printing
When print_twice('Spam') is called, the string 'Spam' is bound to the parameter arg. The function then uses arg in its body two times. First it prints the value represented by arg, and then it prints the same value again. Nothing in this process requires the function to test whether arg is a string.
The integer call follows the same execution pattern. In print_twice(17), the integer 17 is bound to arg. The function prints 17 and then prints 17 again. The function's behavior has not changed; only the value supplied to the parameter has changed.
The floating-point call works in the same way. In print_twice(math.pi), arg is bound to 3.141592653589793, and that value is printed two times. The print function knows how to convert these values to a string for display.
| Call | Value bound to arg | Operation | Result |
|---|---|---|---|
| print_twice('Spam') | 'Spam' | Print arg two times | 'Spam' appears two times |
| print_twice(17) | 17 | Print arg two times | 17 appears two times |
| print_twice(math.pi) | 3.141592653589793 | Print arg two times | 3.141592653589793 appears two times |
The value changes between calls, but the function's operation remains the same.
Operation-Based Compatibility
A data type is compatible with a function when the function's operations can successfully be performed on values of that type.
To determine whether another type will work, inspect the function's operations rather than guessing from the parameter name. For print_twice, the relevant operation is printing. Test the value with that operation and check whether every operation in the function succeeds.
Checking for Failure
The fact that print_twice accepts strings, integers, and floats does not mean that every possible value must work. Its requirement is that the value be printable. The source explains that strings, integers, floats, booleans, and many other types can be printed, while a value that the print function cannot handle would cause the function to fail.
Common Reasoning Mistakes
Assuming a function accepts only the type used in its first example.
The function's operation is printing, and the same operation also works with the integer 17 and the floating-point value 3.141592653589793.
Fix:
Inspect what the function does with its argument and check whether that operation works with the proposed type.Expecting the function to behave differently because the argument type changes.
In both calls, the value is bound to arg and then printed two times.
Fix:
Trace the binding and the function body separately. The value changes, but the operation sequence stays the same.Assuming every data type is automatically compatible.
The constraint is that the value must be printable. A value that the print function cannot handle would cause failure.
Fix:
Test the type with the function's operations and verify that all of them succeed.
Practice the Trace
For each call, identify the value bound to arg, state the operation performed by print_twice, and predict what appears as output: print_twice('Hello'), print_twice(42), and print_twice(2.5). Then explain why the same reasoning applies to all three calls.
Hints
- Start by identifying the argument value in each call.
- The function performs the same operation twice for every call.
- Ask whether the value can be printed.
Tracing a New String Argument
Predict the behavior of print_twice('Hello').
Bind: The string 'Hello' is bound to the parameter arg.
First operation: The function prints the value represented by arg.
Second operation: The function prints arg again.
The string 'Hello' is printed two times because it can be handled by the function's printing operation.
Key Takeaways
- A function can accept different data types when its operations are compatible with those types.
- print_twice works with strings, integers, and floats because each value can be printed.
- Changing the argument changes the value bound to the parameter, not the function's sequence of operations.
- To verify compatibility, test the argument type and check whether every operation in the function succeeds.
Key Takeaways
- Function compatibility comes from the operations performed on an argument.
- print_twice prints strings, integers, and floating-point numbers two times.
- Tracing a call means following the argument into the parameter and then through each operation.
- A type is suitable only when every operation required by the function can handle its value.