Decorators are a type of special function that allows us to add functionality to an object without modifying it's structure.
When we design a decorator I like to think of it akin to Christmas wrapping. Don't leave just yet, let me explain! First to break it down we have a function stored within another function.
The first function is our decorator function.
The second function is our stored function or rather the inner function is our callback function that we use.
The third function is our caller function.
To use our caller function we need a variable set up to actually get our function within the decorator.
Here we create an arbitrary name that sets everything in place for us to have our function become fully fleshed out.
Here we have our variable store our decorator function. Get_this = decorator(function name)
Within our decorator function we pass our third function into it as a callback function.
This tells our decorator function, that the func potion is now taking in our get called() function and stores it in the 2nd func field.
Now all we have to do is call our stored function.
Imagine you have a box but with that box you want to store a christmas gift for a friend, sweetheart, or a family member.
Our gift is the function we're calling within the second function, that also says that person's name.
Our second function is the box that's keeping our gift safe, where we use our prints() as stuffing paper.
Our decorator function is the nice note / wrapping paper that says the person's name as well.
Our variable serves as the tape on the outside to keep our wrapping paper working and pretty.
When we call our function that's done from the message on the wrapping paper.
Now let's say we ran out of wrapping paper and only had enough to our 5 most important people for the holiday season. Here is where decorators are very special, we can reuse the same type of box, and wrapping paper to put a different gift inside of it for each of our very important people! I hope this was a good way to help you understand Decorator functions.
A decorator is a function that takes another function as an argument and returns a new function, often attaching pre- or post-processing functionality.
_ are referred to as underlines and mean our variable is private
We can use try in our entire call to attempt to push the code.
Then we can use except ValueError as error:
Print(f"Error: {str(error)}")
rpr representation method
@classmethod
Def print_player_names(cls): #class represents class
For player in cls.all:
Print(player.name)
Player.print_player_names()
Classmethods always should call on the class and not the individual item stored in the class. When calling a class capitalize it exactly as it is written in the class.
Top comments (0)