2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -6,7 +6,7 @@
In [1]:
```
```py
def foo(x):
print x
@@ -14,7 +14,7 @@ print(type(foo))
```
```
```py
<type 'function'>
```
@@ -23,14 +23,14 @@ print(type(foo))
In [2]:
```
```py
dir(foo)
```
Out[2]:
```
```py
['__call__',
'__class__',
'__closure__',
@@ -68,12 +68,12 @@ Out[2]:
In [3]:
```
```py
foo.__call__(42)
```
```
```py
42
```
@@ -82,12 +82,12 @@ foo.__call__(42)
In [4]:
```
```py
foo(42)
```
```
```py
42
```
@@ -96,7 +96,7 @@ foo(42)
In [5]:
```
```py
def bar(f, x):
x += 1
f(x)
@@ -105,12 +105,12 @@ def bar(f, x):
In [6]:
```
```py
bar(foo, 4)
```
```
```py
5
```
@@ -121,7 +121,7 @@ bar(foo, 4)
In [7]:
```
```py
def dec(f):
print 'I am decorating function', id(f)
return f
@@ -132,12 +132,12 @@ def dec(f):
In [8]:
```
```py
declen = dec(len)
```
```
```py
I am decorating function 33716168
```
@@ -146,14 +146,14 @@ I am decorating function 33716168
In [9]:
```
```py
declen([10,20,30])
```
Out[9]:
```
```py
3
```
@@ -161,7 +161,7 @@ Out[9]:
In [10]:
```
```py
def loud(f):
def new_func(*args, **kw):
print 'calling with', args, kw
@@ -174,19 +174,19 @@ def loud(f):
In [11]:
```
```py
loudlen = loud(len)
```
In [12]:
```
```py
loudlen([10, 20, 30])
```
```
```py
calling with ([10, 20, 30],) {}
return value is 3
@@ -194,7 +194,7 @@ return value is 3
Out[12]:
```
```py
3
```
@@ -206,7 +206,7 @@ Out[12]:
In [13]:
```
```py
def foo(x):
print x
@@ -214,7 +214,7 @@ foo = dec(foo)
```
```
```py
I am decorating function 64021672
```
@@ -223,21 +223,21 @@ I am decorating function 64021672
In [14]:
```
```py
@dec
def foo(x):
print x
```
```
```py
I am decorating function 64021112
```
事实上,如果修饰符返回的是一个函数,那么可以链式的使用修饰符:
```
```py
@dec1
@dec2
def foo(x):
@@ -249,7 +249,7 @@ def foo(x):
In [15]:
```
```py
@loud
def foo(x):
print x
@@ -258,12 +258,12 @@ def foo(x):
In [16]:
```
```py
foo(42)
```
```
```py
calling with (42,) {}
42
return value is None
@@ -276,7 +276,7 @@ return value is None
In [17]:
```
```py
def plus_one(f):
def new_func(x):
return f(x) + 1
@@ -293,7 +293,7 @@ def times_two(f):
In [18]:
```
```py
@plus_one
@times_two
def foo(x):
@@ -303,14 +303,14 @@ def foo(x):
In [19]:
```
```py
foo(13)
```
Out[19]:
```
```py
27
```
@@ -320,7 +320,7 @@ Out[19]:
In [20]:
```
```py
def super_dec(x, y, z):
def dec(f):
def new_func(*args, **kw):
@@ -335,7 +335,7 @@ def super_dec(x, y, z):
In [21]:
```
```py
def super_loud(filename):
fp = open(filename, 'w')
def loud(f):
@@ -355,7 +355,7 @@ def super_loud(filename):
In [22]:
```
```py
@super_loud('test.txt')
def foo(x):
print x
@@ -366,12 +366,12 @@ def foo(x):
In [23]:
```
```py
foo(12)
```
```
```py
12
```
@@ -380,20 +380,20 @@ foo(12)
In [24]:
```
```py
with open('test.txt') as fp:
print fp.read()
```
```
```py
calling with(12,){}
```
In [25]:
```
```py
import os
os.remove('test.txt')