Function Parameters and Typing - Python 3 Programming Tutorial p.7
Based on sentdex's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Python function parameters can be required, passed positionally, passed by keyword, or made optional using default values.
Briefing
Function parameters in Python aren’t just about passing values—they’re the mechanism that lets a single function handle multiple situations, from “show the board” to “apply a move.” The core takeaway is that Python functions can require positional arguments, accept them in a specific order, or make them optional via defaults. That flexibility becomes especially useful in a game-board function, where the same routine might be called to render the board at the start of a match and later to mark a player’s chosen row and column.
The tutorial starts with a simple example: a function that takes two parameters, x and y, and returns x + y. It then demonstrates what happens when parameter types change. Adding two integers works (5 + 3 → 8), and concatenating two strings also works (e.g., “hey” + “there” becomes “heythere”). But mixing integers and strings triggers an error because Python doesn’t have a universal “+” behavior across incompatible types. This leads into Python’s dynamic typing: variables don’t carry declared types, so the program must be ready for different types at runtime. While Python allows type annotations, the enforcement of types is uncommon in everyday code; the tutorial notes that tools like Cython (referred to as “scythe on” in the transcript) can use typing information to compile Python into faster C-like code.
From there, the focus shifts to a game board function that needs parameters such as player, row, and column. When those parameters are required, calling the function without them produces a clear runtime error: Python reports missing required positional arguments and even indicates which ones. If only one argument is provided, the function still fails because it expects the full set in order. Passing values positionally works—for example, calling game_board(1, 2, 0) to represent player 1 at row 2, column 0.
However, hard-coding positional values becomes hard to read in larger programs. The tutorial shows how keyword arguments improve clarity: game_board(player=1, row=2, column=0) makes the intent obvious. It also demonstrates that keyword arguments can be mixed with positional ones (as long as the order rules are followed), and that defaults can make parameters optional. By setting defaults like player=0, row=0, and column=0, the function can be called with no arguments to simply display the board.
To avoid corrupting the board when the function is used for display-only calls, the tutorial introduces conditional logic. Instead of always writing player values into the board array, it uses a flag (such as display) or checks whether player is nonzero before applying a move. This prevents overwriting a real move at position (0,0) when the function is invoked just to render the current state.
Finally, the tutorial warns about mutating objects defined outside a function. Modifying external state can work at first, but it often leads to confusing bugs later unless mutability and data flow are handled carefully—topics saved for the next installment.
Cornell Notes
Python function parameters determine how a single routine can handle different tasks, such as rendering a game board versus applying a move. Required parameters must be passed, and Python enforces this by raising errors for missing positional arguments. Keyword arguments (e.g., player=1, row=2, column=0) improve readability, while default parameter values (e.g., player=0) let the function run without inputs. Because Python is dynamically typed, the behavior of operations like “+” depends on runtime types, so mixing incompatible types (like int and str) raises errors. For game logic, conditional checks or a display flag prevent accidental overwrites when the function is called just to show the board.
Why does Python raise an error when adding an integer and a string as function parameters?
What does it mean that Python is dynamically typed, and how does that affect function behavior?
How do required function parameters behave when a call omits them?
Why are keyword arguments useful for a game_board function?
How do default parameter values enable a “display-only” call to game_board?
What strategy prevents accidental board overwrites when game_board is called without a move?
Review Questions
- How would you rewrite a call to game_board using keyword arguments to make it clear which value is the player, which is the row, and which is the column?
- What runtime error would you expect if game_board requires three positional parameters and you call it with only one argument?
- What conditional check would you add to ensure game_board() doesn’t overwrite the board when it’s used only to display the current state?
Key Points
- 1
Python function parameters can be required, passed positionally, passed by keyword, or made optional using default values.
- 2
The “+” operator depends on operand types; int+int works, str+str works, but int+str raises an error.
- 3
Python’s dynamic typing means type compatibility issues often surface at runtime when operations are attempted.
- 4
Keyword arguments improve readability by labeling player, row, and column directly in function calls.
- 5
Default parameter values let a function run for “view” use cases, such as rendering a game board at the start.
- 6
Game logic should include a display flag or conditional checks to prevent overwriting board state during display-only calls.
- 7
Mutating external objects inside functions can lead to confusing bugs later, making mutability an important concept to study next.