第二十七天
1.主题:专题强化
Modules + O/i + Erros and Exceptions + Class
2.摘要
模块导入要分别对待,不能用from … import *这种全部导入的方式,要考虑内存的占用与代码的运行速度,只导入包的上层模块,对于下层子模块,用到哪个就导入哪个;
str.zfill(n)负责在字符串前加‘0’,直到位数满足n位,±视为一个位,当原始字符串超出n位,则返回原始字符串,并不做加零处理。比如
>>> len('gotohellyou'.zfill(5))
11
>>> 'gotohellyou'.zfill(5)
'gotohellyou'
>>> 'ours'.zfill(6)
'00ours'
文件打开是,mode为a,表明是appending,即文末写入新内容;
It is good practice to use the with keyword when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point. Using with is also much shorter than writing equivalent try-finally blocks:
>>>
>>> with open('workfile') as f:
... read_data = f.read()
>>> f.closed
True
If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it. If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times.
这段话告诉我们:
a.打开文件用with比较安全,能保证正常关闭;
b.用with语句,比用try-finally语句块占地少;
c.读取完数据立即关闭文件释放内存,可以提升计算机的性能,避免被不同程序反复清理文件缓存;
Error —> SyntaxError语句结构出错,不规范
Exceptions —> 运行中异常中止,无法继续,包括多种异常
_init_()负责采集参数值传给类中的self变量
类class = 概念框架
实例instance object = 案例化
属性data attribute = 案例的参数
方法method = 由类的参数决定实例所具有的功能,也就是实例的函数之类的
Generally speaking, instance variables are for data unique to each instance and class variables are for attributes and methods shared by all instances of the class。
类的属性 —> 共性,必须不可更改,即immutable,故不能用list与字典;
实例的属性 —> 个性,可更改,即mutable,可以用list与字典;
Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.
==>method中的self实际上就是传统习俗,其实并无卵用,但又不能不写,这就好比过年时CCTV的春节晚会,有没有它都是春节,但要是不全家嗑瓜子一起胡乱看上两眼总觉得不是那回儿事;
Each value is an object, and therefore has a class (also called its type). It is stored as object.__class__.
—>python中一切所见结尾实体object,皆流淌着其群族的特征,可通过type()查询;
The random module provides tools for making random selections:
>>>
>>> import random
>>> random.choice(['apple', 'pear', 'banana'])
'apple'
>>> random.sample(range(100), 10) # sampling without replacement
[30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
>>> random.random() # random float
0.17970987693706186
>>> random.randrange(6) # random integer chosen from range(6)
4
3.心得感悟
A.代码编写只是动刀动枪,而框架设计才是顶层策略才能统战全局;
B.模块就是一块积木,就看你想搭建什么,要取用那些积木块,用什么排列组合进行编排组装;
C.从generator与循环语法对比来看,要考虑计算机的内存效率,编程的目的就在于优化设计一套业务程序化流程,做到低成本高效能,这和企业成本控制与战略管理如出一辙,可见各学科都是顶层思路相通;
D.类Class给我的感觉就是国际范儿名模,比较抽象高大上,Object就是眼前人,是本地化的真人,只不过这个本地人穿着超模的衣服;
E.阅读python官网document真是享受,说的有道理有例子,言简意赅,直击重点,但也是难啃啊!
F.牢记根源The Python Standard Library¶
https://docs.python.org/3/library/index.html#library-index
4.时间统计
昨日阅读5小时,累计240小时
|