This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Introduction

While going through our course, you may have already stumbled upon labels like @property, @classmethod, @abstractmetod. These are usually found perched comfortably before function definitions.

So what exactly are these labels? They have a name - these are called decorators.

So what do these decorators do?

Well, before we answer that, let us first talk about functions.

Inner/Nested functions

The first point you need to know: in Python, you can define another function inside a function. These are called inner or nested functions.

def outer(text):
    print("Inside outer")

    def inner():
        print("Inside inner")
        print(text)
        print("Exiting inner")

    print("Calling inner")
    inner()
    print("Exiting outer")

outer("Hello!")

You would usually not have any need to do this, so please avoid doing this unless you have a good reason to do so!

One possible reason is encapsulation, that is to hide the inner function so that you cannot call the inner function from outside the function definition of outer (that was a confusing sentence!)

Try calling inner() after outer("Hello!") in the code above. It should not work!