Felt cute, so took this timed test on DataCamp today. This just validates how far I have come since first starting to code in Python. Sometimes I need these small “affirmations” when I feel down. Anyhow the focus of this blog is to list the questions that I got wrong, and some resources to work on it so that I (or anyone reading this) can come back to it later.
Wrappers
Python wrappers are not something that I use a lot. These weird looking “@something” that decorates function definitions are not at all complicated. They simply extend an existing Python function to add some new functionality to suit your use case. Got first introduced to the inner-workings of these things after Jeremy Howards fast.ai book/course. There are some neat wrappers and class decorators from the course (typedispatch
for instance) which are also available as a package called fastcore. Wrappers were also recommended in the book High Performance Python to time functions, like so:
import time
def timer(func):
def func_timer(*args, **kwargs):
start = time.time()
func(*args, **kwargs)
print(f'time to run {func.__name__}: {time.time()-start}')
return func_timer
@timer
def square(x):
"""Get the square of x"""
return x*x
Resources: GeeksforGeeks, RealPython
Attributes
It hurt when I got this wrong. The question was to write how to get the docstring
of a function. This does not need a lot of tutorials, just remember (I did not) to do square.__doc__
. Using the same function from the above code snippet. To list all the attributes of the function we can also do dir(square)
. Remember that everything in Python is an object and objects have a lot of nice attributes and methods.
That’s it, Cheers!