Working with Strings, Integers, and Floats
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.
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
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
Common mistake Assuming a function accepts only the type used in its first example.
What it looks likeSeeing print_twice('Spam') and concluding that print_twice accepts strings only.
Why it failsThe function's operation is printing, and the same operation also works with the integer 17 and the floating-point value 3.141592653589793.
Do this insteadInspect what the function does with its argument and check whether that operation works with the proposed type.
Common mistake Expecting the function to behave differently because the argument type changes.
What it looks likePredicting that print_twice(17) will perform a different sequence of operations from print_twice('Spam').
Why it failsIn both calls, the value is bound to arg and then printed two times.
Do this insteadTrace the binding and the function body separately. The value changes, but the operation sequence stays the same.
Common mistake Assuming every data type is automatically compatible.
What it looks likeBelieving that any value can be passed to print_twice without checking whether printing can handle it.
Why it failsThe constraint is that the value must be printable. A value that the print function cannot handle would cause failure.
Do this insteadTest 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').
- 1Bind
The string 'Hello' is bound to the parameter arg.
- 2First operation
The function prints the value represented by arg.
- 3Second 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
- 1A function can accept different data types when its operations are compatible with those types.
- 2
print_twiceworks with strings, integers, and floats because each value can be printed. - 3Changing the argument changes the value bound to the parameter, not the function's sequence of operations.
- 4To verify compatibility, test the argument type and check whether every operation in the function succeeds.
At a glance
- Function compatibility comes from the operations performed on an argument.
print_twiceprints 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.