Beyond the Basics of Function
Python tutorial · PySpark.in
1 Anonymous (Lambda) Functions
- Small, one-line functions without a name.
- Defined using the lambda keyword.
- Commonly used for short, throwaway tasks or as arguments in other functions.
Use Case: Useful in functions like map(), filter(), and sorted().
2 Recursion (Function Calling Itself)
- A recursive function is one that calls itself until a base condition is met.
Use Case: Solving problems like factorial, Fibonacci, or tree traversal.
3 Scope and Lifetime of Variables
- Local variables → exist only inside the function.
- Global variables → can be accessed from anywhere.
Use Case: Understanding variable access and avoiding name conflicts.
4 The global and nonlocal Keywords
- global → allows modifying global variables inside a function.
- nonlocal → allows modifying a variable in the enclosing (non-global) scope.
5 Docstrings (Documentation Strings)
- Used to describe what a function does.
- Written just below the function definition inside triple quotes """.
Use Case: Improves readability and helps tools like help() and IDE tooltips.
6 Function Annotations (Type Hints)
- Help specify expected data types for parameters and return values.
Use Case: Improves code clarity and helps static type checkers.
7 Higher-Order Functions
- Functions that take other functions as arguments or return functions.
Use Case: Common in functional programming and frameworks.
8 Closures
- Inner functions that “remember” variables from the outer function even after the outer function has finished executing.
Use Case: Useful for decorators and encapsulation.
9 Decorators
- Special functions that modify the behavior of other functions without changing their code.
Use Case: Logging, authentication, timing, etc.
10 Built-in Higher-Order Functions
These built-in functions take other functions as arguments:
- map(function, iterable)
- filter(function, iterable)
- reduce(function, iterable) (from functools)
- sorted(iterable, key=function)
11 Generator Functions & yield Keyword
- Used to create iterators.
- Instead of returning all values at once, they yield one value at a time.
Use Case: Memory-efficient iteration.
12 Function Introspection
You can get information about a function using built-in attributes:
- __name__, __doc__, __defaults__, __annotations__, etc.
13 Partial Functions (from functools)
- Allow you to “fix” some arguments of a function and create a new one.
Use Case: Simplify frequently used function calls.
14 Async (Asynchronous) Functions
- Introduced with async def, used with await keyword for concurrency.
Use Case: For non-blocking I/O operations and web apps.
More Python tutorials
- What is Python and Why is it used for Data Science and Data Engineering?
- How Does Python Work in the Backend? Internal Working of Python
- Top 30 Python Interview Questions for Data Science
- 3-Month Python Roadmap to Excel in Data Science and Machine Learning
- Python Data Types Explained – A Beginner’s Guide
- test
All tutorials · Try the free PySpark compiler · Practice challenges