View on GitHub

reading-notes

Daily Reading 8

Ten Thousand 3

What is the basic syntax of Python list comprehension, and how does it differ from using a for loop to create a list? Provide an example of a list comprehension that squares the elements in a given list of integers.

List comprehension is a Python list feature that allows you to modify the contents of a list by defining a mathematical operation inside the square brackets of your reference to an existing list. In many ways, it is a more concise and readable way to do a for-loop to iterate through the list.


inputList = [1,2,3,4,5]
outputList = [item**2 for item in inputList]

(item**2 is the notation for squaring a number)

The line of code for outputList is creating a new list with new values by multiplying an item in the input list by itself, and saving that value as outputList’s item in that index. So the 1 at index 0 in the inputList is multiplied by 1, resulting in 1, which is saved in outputList’s index 0. The 2 at index 1 is multiplied by 2, resulting in 4 being saved at index 1 of outputList, and so on. Overall, using list comprehension is very often much more concise and readable than a bespoke for-loop exclusively for performing actions on all elements of a list.

What is a decorator in Python?

A decorator is an “outer” function that wraps around an existing “inner” function, in order to take some additional action before, or after. Decorators can also be used for modifying the inner function as it is executed, such as running it twice for a simple example. They are functions that take in functions as arguments, in order to run the inner function with some form of change applied.

Explain the concept of decorators in Python. How do they work, and what are some common use cases for them? Provide an example of a simple decorator function from the reading.

The reading of the day was Primer on Python Decorators, by Geir Arne Hjelle from realpython.com. One of the examples provided of a decorator in use, using the pie notation (@) is excerpted below.

def my_decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@my_decorator
def say_whee():
    print("Whee!")

When say_whee() is called during runtime, instead of directly calling say_whee, instead the @my_decorator line is noticed, and instead, for practical purposes, the following operation is performed instead:

my_decorator(say_whee())

which will provide the output of:

Something is happening before the function is called.
Whee!
Something is happening after the function is called.

Once again, the above example is what happens when say_whee() is run alone, without explicitly declaring the outer wrapping of my_decorator().

With the use of the decorator, additional functionality has been added to say_whee() without needing to directly change the contents of say_whee(), and additional wrapping functionality has been provided without needing to explicitly ask for it through a function call.

Back To Main Page.