Python Decorators Lecture Notes PDF
Document Details
Uploaded by Deleted User
Iscte-Sintra
2024
Tags
Summary
This document is a lecture note about Python decorators. It details how decorators add functionality to Python functions and provides example code. Exercises are also available on Moodle for practice.
Full Transcript
Programação Orientada a Objetos (Python) Class #4.2 – Decorators ISCTE Sintra 2024/2025 Outline for this class [C4.2] Class 4 CP7: Decorators [C4.2] 25/01/2024 2 Decorators A decorator is a design pattern in Python that allo...
Programação Orientada a Objetos (Python) Class #4.2 – Decorators ISCTE Sintra 2024/2025 Outline for this class [C4.2] Class 4 CP7: Decorators [C4.2] 25/01/2024 2 Decorators A decorator is a design pattern in Python that allows a user to add new functionality to an existing object without modifying its structure. Decorators are typically applied to functions, and they play a crucial role in enhancing or modifying the behavior of functions. Decorators are placed before the definition of a function you want to “decorate”. Functions in Python are first class citizens ☺ This means that they support operations such as being passed as an argument, returned from a function, modified, and assigned to a variable. This property is crucial as it allows functions to be treated like any other object in Python ☺ 25/01/2024 3 Decorators Let’s create a typical function that will add one to a number whenever it is called. We'll then assign the function to a variable and use this variable to call the function: Output 6 25/01/2024 4 Decorators Now, let’s illustrate how to define a function inside another function in Python ( we will shortly know why we need to do this ☺): Sometimes, inner functions are called wrapper functions ☺ We can also pass functions as arguments to other functions ☺ 25/01/2024 5 Decorators A function can also generate another function ☺ Python allows a nested function to access the outer scope of the enclosing function. This is a critical concept in decorators ☺ 25/01/2024 6 Decorators With the necessary background covered, we'll now proceed to construct a basic decorator that capitalizes a sentence (transform all word initials in uppercase). This is achieved by implementing a wrapper function within an outer function. This approach closely resembles the earlier example where we nested one function within another. 25/01/2024 7 Decorators Our decorator function takes a function as an argument, and we need to define a function and pass it to our decorator. We'll use that trick to call our decorator function ☺ However, Python provides a much easier way for us to apply decorators. We simply use the @ symbol before the Output function we'd like to decorate ☺ 25/01/2024 8 Decorators Sometimes, we might need to define a decorator that accepts arguments. We achieve this by passing the arguments to the wrapper function. The arguments will then be passed to the function that is being decorated at call time. 25/01/2024 9 Decorators We can also use general purpose decorator that can be applied to any function. We can use *args and **kwargs to allow us to pass as many arguments as we would like during function calls. 25/01/2024 10 Exercises Open Moodle and access Autonomous Work exercises about: C4: Functions with Variable Arguments & Decorators File: IUR-POO-Class4-Exercises.pdf 25/01/2024 11 Next class [C5] CP8: Graphical Interface 25/01/2024 12