python 学习汇总59:高阶函数与类的关系(初级学习- tcy)

 目录: 
1. class定义
2. 内部类
3.外部定义函数
4.高阶函数与类的关系
5.对象内存管理
6.类作用域
7.使用输出参数
8.类属性
9.类特性
10.描述符
11.查看类属性
12.继承
13.类型检测测试,查看父子类
15.元类
16.基类
17.类装饰器
18.Enum类
其他参考本人博文。 

高阶函数与类的关系 创建日期2018/8/7 修改时间2018/11/19

1.创建高阶函数:

def linear(a, b):
def result(x):
return a * x + b
return result

# 测试:
taxes=linear(0.3,2)
taxes(10e6)#3000002.0
2.创建类:缺点是速度稍慢,代码稍长 : 
class linear:
def __init__(self, a, b):
self.a, self.b = a, b
def __call__(self, x):
return self.a * x + self.b

# 调用
taxes = linear(0.3, 2)
taxes(10e6) == 0.3 * 10e6 + 2#True
taxes(10e6) # 3000002.0
3.继承分享他们的签名:  
class exponential(linear):
# __init__ inherited #不用再初始化
def __call__(self, x): #实现新功能
return self.a * (x ** self.b)

#调用
a=exponential(2,3)
a(2) #16 =2*(2**3) 
4.可以封装几种方法: 
class counter:
value = 0
def set(self, x):
self.value = x

def up(self):
self.value = self.value + 1

def down(self):
self.value = self.value - 1

count = counter()  

猜你喜欢

转载自blog.csdn.net/tcy23456/article/details/84259905