工作流程:
- 组织大型项目的代码应一开始使代码结构尽可能简单,尽量在一个文件完成所有的工作
- 待工作能够正常运行后,再将类移到独立的模块中
2018.05-2018.09
Python编程:从入门到实践
chapter 9.3.4-9.3.5
- 1.继承父类具体用法(类实例化时首先默认运行__init__(),初始化预先设定的属性值(绑定到实例))
- 2.super()是一个特殊函数,用于关联父类(被继承)和子类(继承类),实际可将super()看做父类在局部的实例,如super().__init__(父类除self以外的形参)
- 3.用 类A的实例 作为 B类的属性
1 class Restaurant(object): 2 def __init__(self,name,catering_type): 3 self.restaurant_name = name 4 self.catering_type = catering_type 5 self.dinner_table = 20 6 self.state = True 7 8 def describe_restaurant(self): 9 print( "\nbasic describe:" 10 " restaurant name:"+self.restaurant_name + 11 "\ncook type:"+self.catering_type) 12 13 def restaurant_state(self): 14 if self.state : 15 print("restaurant is now openning.") 16 else : 17 print("not openning yet~") 18 19 def set_number_served(self,number): 20 self.dinner_table = number 21 print(self.dinner_table) 22 23 def increment_number_served(self,consume_people_number): 24 self.dinner_table += consume_people_number 25 return self.dinner_table 26 27 class TableWare(object): 28 def __init__(self,chopsticks_num=None): 29 self.chopsticks_num = chopsticks_num 30 31 def describe_tableware(self): 32 print("This restaurant has "+str(self.chopsticks_num)+" pairs of chopsticks") 33 34 class SpecificRestaurant(Restaurant): 35 def __init__(self,name,catering_type): 36 super(SpecificRestaurant,self).__init__(name,catering_type) 37 self.tableware = TableWare(100)
37行 TableWare(100)实例化赋值绑定到 self.chopsticks_num = 100
使得TableWare()的类方法与属性值 打包 成为了SpecificRestaurant()的属性 (准确一点说,该实例存储在属性self.tableware中),每次SpecificRestaurant()实例化时,即__init__被调用,都执行实例储存的操作 self.tableware = TableWare(100),所以每个SpecificRestaurant实例都包含一个自动创建的TableWare实例
CNrestaurant =SpecificRestaurant("China chincken","chinese restaurant") CNrestaurant.tableware.describe_tableware()
创建一个restaurant实例,要描述tableware的时候,使用该属性即可
- 4.导入类
#假定 以上程序存为restaurant.py 在另一个新.py里导入restaurant.py则有 from restaurant import Restaurant,SpecificRestaurant #从该脚本引入具体的类或多个类(函数)
#如果要导入整个模块
import restaurant
#导入模块中的所有类
from restaurant import *
- 5.驼峰命名法:类名的每个独立单词首字母大写,不使用下划线,而实例和模块名都使用小写格式,并推荐使用下划线做分隔
- 6.文档字符串:对于每个类都需要包含一个文档字符串,并对所包含的函数(或类方法)的参数和返回值进行描述,引例,如下
#coding:utf-8 import numpy as np class TensorFactory(): """ A factory that creates tensors. """ def Build(self, shape, datatype, value): """ Args: raw_shape: the configuration of tensor shape, e.g. [2,2,2] or ["rand", "rand", "rand"] datatype: the configuration of tensor data-type e.g. np.float32 value: the configuration of tensor value, e.g.'rand' or int Returns: A tensor for tensorflow , shape of tensor. """
2018.10
利用Python进行数据分析 (速刷完)
2018.12
深度学习入门:基于Python的理论与实现(速刷完)
2019.01.09开始
Tensorflow 实战Google深度学习框架 第二版