Skip to main content

11 docs tagged with "functions"

View all tags

*args and **kwargs: Accepting a Variable Number of Arguments

We've learned how to define functions with a fixed number of positional and keyword arguments. But what if you want to create a function that can accept any number of arguments? For example, a function that can sum two numbers, or five, or a hundred. This is where args and *kwargs come in, providing the ultimate flexibility for your function definitions.

Closures: Functions with a 'Memory'

We are now at the final article in our series on iterators, generators, and decorators. We will explore closures, a concept that is fundamental to how decorators and other advanced patterns work in Python.

Decorators (Part 1): Introduction to Decorators

Welcome to the final topic in our series on advanced Python concepts: decorators. Decorators are one of the most powerful and widely used features in Python, especially in web frameworks like Flask and Django. They might seem magical at first, but they are built directly on top of concepts we already know.

Decorators (Part 2): Decorators with Arguments

In our last article, we learned that a decorator is a function that wraps another function to add new behavior. However, our first decorator was simple: it only worked on functions that took no arguments.

Default Arguments and Keyword Arguments

We've learned how to define functions, pass information to them, and get values back using the [return statement]. Now, let's make our functions more flexible and readable. This article explores two powerful features: default arguments and keyword arguments, which allow you to make function parameters optional and your function calls more explicit.

Defining Functions: `def` Keyword, Parameters, and Arguments

In our previous article, we introduced [Introduction to Functions: The DRY principle and code reusability]. We learned that functions are essential for writing clean, reusable, and maintainable code. This article dives deeper into the mechanics of creating functions, focusing on the def keyword and the crucial distinction between parameters and arguments.

Docstrings and Type Hinting: Documenting Your Functions

Writing code that works is only half the battle. Writing code that you and others can understand months from now is just as important. This is where documentation comes in. In this article, we'll explore two powerful, related concepts for documenting your Python functions: docstrings and type hints. Mastering them will elevate the quality and professionalism of your code.

Function Return Values: The `return` Statement

In our last article, we learned how to define functions and pass information to them using [parameters and arguments]. So far, our functions have only performed actions, like printing to the console. But what if we need a function to give us a result back? This is where the return statement comes in, and it's a game-changer for writing powerful, modular code.

Introduction to Functions: The DRY Principle and Code Reusability

Welcome to a new chapter in your Python journey! We've spent a great deal of time understanding the fundamental building blocks of Python—variables, data types, and control flow. Now, it's time to learn how to organize our code into reusable, efficient, and readable blocks. This article introduces one of the most important concepts in programming: functions.

Lambda Functions: Anonymous, On-the-Fly Functions

We have covered almost every fundamental aspect of Python functions. For our final topic in this series, we'll explore a special type of function: the lambda function. Lambdas, also known as anonymous functions, provide a concise way to create small, single-use functions without the overhead of a full def statement.

Scope: Local, Enclosing, Global, and Built-in (LEGB Rule)

We've been working with variables and functions, but we haven't yet discussed a crucial concept: scope. Scope determines the visibility and accessibility of a variable. In other words, it answers the question, "Where in my program can I access this variable?" Understanding scope is essential for writing bug-free code and avoiding common errors where variables seem to "disappear" or have unexpected values.