This is my note for the book Python Tricks: The Book A Buffet of Awesome Python Features written by Dan Bader from RealPython. Personally, I like RealPython very much. The tutorials on this website are mostly thorough and clear. I would recommend it to you if you want to dive deeper into some python features or functions.
- When do we use
assertand how to use it?
- What is a context manager? How to use
with? How to write a self-defined context manager? (Two ways.)
- List commonly used magic methods, or say, dunder methods, and think about their usage.
- What is a class method? What is a static method? What are the differences?
- Underscore in variable names.
- What are the differences between private variables and protected variables?
- Formatting options for literal string
- Write a function with closures. Write an object that can behave like functions, namely, a callable object.
What is a decorator? How to write a debuggable decorator that accepts arguments? How to use the decorator?
**kwargs. What are the difference between positional and keyword arguments?
- What is an iterable? List five common iterables. Are sets and dictionaries iterables?
- What are the differences between
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import functools def uppercase(func): @functools.wraps(func) def wrapper(*args, **kwargs): original_result = func(*args, **kwargs) modified_result = original_result.upper() return modified_result @uppercase def greet(time): """A greeting function.""" if time == 'morning': print('Good morning!') else: print('Hello!') greet('morning') print(greet.__name__) print(greet.__doc__)
What is a generator? What is an iterator? When would we use them? How to use them? How to write them?
Functions With *args and **kwargs
When we input arguments to a function, we can either input according to the order of the arguments in the definition of the function, or use keywords to specify which argument we are inputting. The former is called positiona arguments and the latter is called keyword argument. Since positional arguments follow strictly the order, so it is proper to use a tuple to represent it. While for keyword arguments, order doesn’t matter, and with the key and value structure, a dictionary would be a good choice.
Classes & OOP
Pythonic Coding Habits
- import packages