It can be useful to encapsulate a function in another function. A decorator is a metafunction that returns another function. This lesson provides an example of how useful this can be.
- [Instructor] A decorator is a form of metaprogramming…and it can be described as a special type of function…that returns a wrapper function.…Here in Komodo I've opened working copy of hello.py…from chapter seven of the exercise files.…First it's important to realize that in Python…everything is an object.…So a function is a type of object.…So if I'm here and I define a function…and I run it.…
So I'm calling the function…and you notice that it prints out this is f1.…But what's interesting here is I can say x equals f1…with the parenthesis and now I'm assigning that function…object to the variable x.…But everything is an object, so a variable is an object too…and I can simply call the function f1 by calling x.…So when I save and run, I get exactly the same result.…So this means that I can do silly things like this.…
So now what I've done, is I've created a function f1…that contains another function f2…and I cannot call f2 directly, because it's scope…is inside the function.…Remember functions do define scope,…locks don't define scope, functions do.…
- Python anatomy
- Types and values
- Conditionals and operators
- Building loops
- Defining functions
- Python data structures: lists, tuples, sets, and more
- Creating classes
- Handling exceptions
- Working with strings
- File input/output (I/O)
- Creating modules
- Integrating a database with Python db-api
Skill Level Intermediate
Python: Programming Efficientlywith Michele Vallisneri2h 15m Intermediate
2. Language Overview
3. Types and Values
8. Structured Data
11. String Objects
12. File I/O
13. Built-in Functions
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.