Urgent.News

What's breaking now, across thousands of outlets.

Tech

Python Decorators: A complete guide to decorators in Python

If you've seen something like @yo_i_am_decorating_this_func above a Python function and wondered what's going on, you're in the right place. Python decorators look like magic at first, but they're built from a few simple ideas. In this guide we'll take one small example and grow it step by step until you understand every part of how decorators work. Our Running Example: A Slow Weather Fetcher…

Decorators in Python are powerful tools that allow you to modify the behavior of functions or methods. This guide provides a comprehensive overview of decorators, starting with a simple example of a slow weather fetcher function. The ultimate goal of decorators is to measure the execution time of the fetch_weather function without altering its code.

First, it's essential to understand that in Python, functions are considered first-class objects. This means that functions can be stored in variables, passed as arguments to other functions, and returned from functions, just like numbers or strings. This feature forms the basis for creating decorators.

A decorator is essentially a function that takes another function as its argument and returns a new version of that function with additional functionality. To create a decorator, we first need to define a function that accepts the original function as an argument. This function will then wrap the original function with additional code, such as timing the function's execution.

One way to implement a decorator is by using a closure. A closure is an inner function that remembers variables from the function that created it. In the context of decorators, closures allow the wrapper function to access and manipulate the original function even after the decorator has been applied. In our example, we define a timer decorator that wraps the fetch_weather function and adds timing functionality to it.

Python also provides a more concise way to apply decorators using the "@" syntax at the beginning of the function definition. This "@timer" syntax accomplishes the same thing as explicitly calling timer(fetch_weather), but it's more readable and less verbose.

When working with decorators, it's important to consider how they handle function arguments. In our example, the timer decorator accepts any number of arguments using the *args and **kwargs syntax. This allows the decorator to work with functions that have varying numbers of parameters, such as a fetcher function that accepts a units parameter.

Finally, decorators can also preserve the original function's metadata, such as its name and docstring. Without this metadata, it becomes difficult to debug or document the decorated function. The functools.wraps decorator is a built-in tool that helps maintain the original function's metadata. By using functools.wraps in your decorator, you ensure that the decorated function retains its original name and documentation.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Why Translating a PDF Before Filling Forms Breaks Fields and Doubles Credit Costs

Sending a raw 25-page vendor packet to a document translator before fixing it locally causes two avoidable problems. First, at 2 credits per page, translating eight appendix pages you meant to delete…

  • Translating PDF before editing creates 16 extra credits for deleted pages.
  • Editing text in translated PDFs may break due to tightly subsetted fonts.
  • Editing in browser first ensures clean PDF before transmitting to recipients.

Why your falling-blocks game feels unfair (and three small fixes)

You can build a falling-blocks game that follows every rule and still have players call it unfair within a minute. The pieces fall, rows clear, the score goes up, but something feels off.

  • Replace pure randomness with shuffled bag of pieces
  • Implement wall kicks for smooth rotation near walls
  • Introduce lock delay to prevent instant death upon landing

Elm for Beginners: Frontend Code With No Runtime Errors

Elm is a functional language for building web frontends, and it makes a bold promise: no runtime errors. If your Elm program compiles, it will not crash in the browser with an undefined value or a…

  • Elm is a functional language for error-free web frontends.
  • Its Model-Update-View architecture simplifies state management.
  • Elm's compiler ensures code compiles correctly, reducing runtime errors.

More from Friday 13 November →