静态方法和类方法
1 class Zfx(object): 2 def __init__(self,a,b,c,d): 3 self.a = a 4 self.b = b 5 self.c = c 6 self.d = d 7 #如果类中需要非该类成员存在,则可以使用静态调用的方法@staticmethod 8 @staticmethod 9 def is_valid(a,b,c,d): 10 for i in [b,c,d]: 11 if i != a: 12 return False 13 else: 14 return True 15 def area(self): 16 if res == True: 17 area_ = self.a * self.b 18 return area_ 19 20 zfx = Zfx(4,4,4,4) 21 res = zfx.is_valid(4,4,4,4) 22 if res ==True: 23 print(zfx.area())
1 @classmethod:获取自身类(cls)中的属性,并且可以更改. 2 classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等 3 """ 4 from time import time, localtime, sleep 5 6 7 class Clock(object): 8 """数字时钟""" 9 10 def __init__(self, hour=0, minute=0, second=0): 11 self._hour = hour 12 self._minute = minute 13 self._second = second 14 15 @classmethod 16 def now(cls): 17 ctime = localtime(time()) 18 return cls(ctime.tm_hour, ctime.tm_min, ctime.tm_sec) 19 20 def run(self): 21 """走字""" 22 self._second += 1 23 if self._second == 60: 24 self._second = 0 25 self._minute += 1 26 if self._minute == 60: 27 self._minute = 0 28 self._hour += 1 29 if self._hour == 24: 30 self._hour = 0 31 32 def show(self): 33 """显示时间""" 34 return ‘%02d:%02d:%02d‘ % 35 (self._hour, self._minute, self._second) 36 37 38 def main(): 39 # 通过类方法创建对象并获取系统时间 40 clock = Clock.now() 41 while True: 42 print(clock.show()) 43 sleep(1) 44 clock.run() 45 46 47 if __name__ == ‘__main__‘: 48 main()
类和类之间的关系
继承和多态
1 #创建一个父类一个子类,父类计算两个数字之和即为Sum,子类打印这个sum 2 class F(object): 3 def __init__(self): 4 self.a = 10 5 self.b = 20 6 def Sum(self): 7 return self.a + self.b 8 class S(F): 9 #第一个 __init__是B自身的 10 def __init__(self): 11 F.__init__(self)#super(A,self).__init__() 12 def Print(self): 13 res = self.Sum() 14 print(res) 15 s_ = S() 16 print(s_.a,s_.b) 17 s_.Sum()
装饰器
1 创建一个装饰器,三个函数(两个参数),装饰器处理这两个参数的和,并打印,每一个函数打印这两个参数 2 """ 3 def deco(func): 4 def warp(a,b): 5 c = a + b 6 print(‘a+b=‘,c) 7 return func(a,b) 8 return warp 9 @deco 10 def sum(a,b): 11 print(a,b) 12 sum(1,2) 13 @deco 14 def sum2(a,b): 15 pass 16 sum2(12,23)
1 # 列表生成式 2 3 a = (x for x in range(100000000000) if x % 2== 0) 4 for i in range(100): 5 print(next(a)) 6 """ 7 列表生成式 8 9 # 列表生成式 10 11 a = [x for x in range(100000000000) if x % 2== 0] 12 优点: 计算速度快,因为一次性已经全部加载到内存中了,适合数据量不是太大的情况10000- 2000- 13 缺点: 占用内存 14 15 # 生成器 16 17 a = (x for x in range(100000000000) if x % 2== 0) 18 优点: 节约内存空间 19 缺点: 计算速度慢,因为要生成. 20 """ 21 import os 22 23 path = ‘/Users/joker/jokers/DataSet/stanford-dogs-dataset/Annotation‘ 24 res = os.listdir(path) 25 print(res) 26 genter = (dir_ for dir_ in res) 27 print(next(genter)) 28 29 # 装饰器 30 31 def Joker(func): 32 33 ? def warp(n1,n2,n3): 34 35 ? num = n1 + n2 36 37 ? return func(0,num,n3) 38 39 ? return warp 40 41 42 43 *装饰器将前两个数字求和,函数本身第三个参数乘上这个和* 44 45 @Joker 46 47 def SUM(num1,num2,num3): 48 49 ? print(num1,num2,num3) 50 51 ? print(num2 * num3) 52 53 54 55 SUM(10,2,3)
函数闭包
创建python虚拟环境
conda create -n your_env_name python=X.X(2.7、3.6等)
anaconda 命令创建 python 版本为 X.X、名字为 your_env_name
的虚拟环境。your_env_name
文件可以在 Anaconda 安装目录 envs 文件下找到。activate your_env_name
(虚拟环境名称)deactivate
原文:https://www.cnblogs.com/byq1/p/11322339.html