-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfun.py
More file actions
111 lines (106 loc) · 1.77 KB
/
Copy pathfun.py
File metadata and controls
111 lines (106 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#coding=utf-8
#非局部变量
# def fun_out():
# a = 4
# def fun_in():
# nonlocal a
# a += 1
# fun_in()
# print a
# fun_out()
a = 1
def out():
b = 2
def inner():
nonlocal b
b += 1
print (b)
inner()
out()
#闭包
# def out():
# a = 1
# print a
# def inner():
# print a+1
# print "I'm inner"
# return inner
#
# f = out()
# f()
#
# def func(name):
# def inner_func(age):
# print 'name:', name, 'age:', age
# return inner_func
#
# b = func('the 5 fire')
# b(26)
#
# #装饰器
# def makebold(fn):
# def wrapped():
# return "<b>" + fn() + "</b>"
# return wrapped
#
# def makeitalic(fn):
# def wrapped():
# return "<i>" + fn() + "</i>"
# return wrapped
#
# @makebold
# @makeitalic
#
# def hello():
# return "hello world"
#
# print hello()
#
# #匿名函数 lambda是一个表达式而不是一个语句,它返回一个函数对象
# L = [lambda x: x ** 2,
# lambda x: x + 3,
# lambda x: x * 4]
#
# for f in L:
# print(f(2))
#==================================
#coding=utf-8
# class TestStaticMethod(object):
# @staticmethod
# def foo():
# print "calling static method foo()"
#
# #foo = staticmethod(foo)
#
# class Child(TestStaticMethod):
# pass
#
# static = TestStaticMethod()
#
# static.foo()
# TestStaticMethod.foo()
#
#
# child = Child()
#
# child.foo()
#
# print "==============================================="
# class TestClassMethod(object):
# @classmethod
# def foo(cls):
# print "calling class method foo()"
# print cls.__name__
#
# class Child1(TestClassMethod):
# pass
#
# cls = TestClassMethod()
#
# cls.foo()
# TestClassMethod.foo()
#
#
# child = Child1()
#
# child.foo()