Function Creation: When creating functions, especially by copying existing ones, be cautious with modifications.
Example: In converting Celsius to Fahrenheit, ensure the return value is appropriate (Celsius should not be returned for a Fahrenheit conversion).
Omission of Return Statement: Forgetting a return statement will cause the function to return None
by default, leading to unexpected behavior.
Example: If a function expected to return an integer returns None
, it can affect the workflow negatively.
Correct Output: A return statement is necessary to pass values back from functions.
Example: A function called without a return will result in a variable holding a None
value rather than the intended result.
Scope Definition: A variable defined within a function is local and not accessible outside the function.
Example: total_inches
within the function height_US_to_centimeters
cannot be accessed globally.
Local vs. Global Variables:
Local Variables: Declared within a function and only accessible in that scope.
Global Variables: Declared outside functions; accessible throughout the entire code.
Modification of Global Variables: Use the global
keyword if you need to modify a global variable from within a function.
Example: global employee_name
allows modification of a global variable within a function.
Define Before Use: Functions must be defined before they are called.
Example: Calling a function defined at the bottom of the code will lead to an error stating it is not defined.
Namespace Concept:
Variables can exist in various scopes (local, global) and may share names.
Python resolves these by first looking for local variable definitions.
Global Namespace: A table of all variables defined at the global level which can be printed using the globals()
function.
Local Scope: Active within a function.
Global Scope: Includes all globally defined names outside any functions and lasts until the end of the file.
Built-in Scope: Contains all reserved names and functions available in Python.
Passing Arguments: Arguments passed to functions are done by object reference (passed by assignment).
Modification of mutable objects (like lists) affects the original, while immutable types (like integers) do not.
Example of Modifying Lists: Modifying a list within a function affects the original list due to mutability.
Calling Functions with Default Values: Default values help simplify function calls by using predetermined values when parameters are omitted.
Example: def print_date(day, month, year, style=0)
allows for flexible calls.
Arbitrary Argument Lists: Use *args
to accept an unknown number of positional arguments, accessible as a list within the function.
Example: Using *args
in def make_sandwich(bread, meat, *args)
allows additional toppings to be handled dynamically.
Keyword Arguments: Use **kwargs
to accept keyword arguments, available as a dictionary within the function.
Example: In def print_order(**kwargs)
, keywords can be processed systematically.
Docstrings Usage: Include docstrings immediately after function definitions to describe their functionality, enhancing code readability.
Example: Using triple quotes to document what the function does and any parameters it accepts.