Browse Source

Format, Decorator, Class

pull/10/head
Jure Šorn 6 years ago
parent
commit
d237d07dea
1 changed files with 7 additions and 3 deletions
  1. 10
      README.md

10
README.md

@ -338,10 +338,10 @@ Format
``` ```
### String Options ### String Options
**`'!r'` calls object's repr() method, instead of format(), to get a string.**
```python ```python
{'abcde'!r:<10} # "'abcde' " {'abcde'!r:<10} # "'abcde' "
``` ```
* **`'!r'` calls object's repr() method, instead of format(), to get a string.**
```python ```python
{'abcde':.3} # 'abc' {'abcde':.3} # 'abc'
@ -608,7 +608,7 @@ def function_that_gets_passed_to_decorator():
``` ```
### Debugger Example ### Debugger Example
**Decorator that prints function's name every time it gets called. Wraps is a helper decorator that copies metadata of function add() to function out(). Without it `'add.__name__'` would return `'out'`.**
**Decorator that prints function's name every time it gets called.**
```python ```python
from functools import wraps from functools import wraps
@ -624,6 +624,8 @@ def debug(func):
def add(x, y): def add(x, y):
return x + y return x + y
``` ```
* **Wraps is a helper decorator that copies metadata of function add() to function out().**
* **Without it `'add.__name__'` would return `'out'`.**
### LRU Cache ### LRU Cache
**Decorator that caches function's return values. All arguments must be hashable.** **Decorator that caches function's return values. All arguments must be hashable.**
@ -714,7 +716,9 @@ class MyComparable:
``` ```
### Hashable ### Hashable
**Hashable object needs both hash() and eq() methods and it's hash value should never change. Objects that compare equal must have the same hash value, meaning default hash() that returns `'id(self)'` will not do. That is why Python automatically makes classes unhashable if you only implement eq().**
* **Hashable object needs both hash() and eq() methods and it's hash value should never change.**
* **Objects that compare equal must have the same hash value, meaning default hash() that returns `'id(self)'` will not do.**
* **That is why Python automatically makes classes unhashable if you only implement eq().**
```python ```python
class MyHashable: class MyHashable:

Loading…
Cancel
Save