Python相关系列

Python相关系列

hello,这里是我大二上下学期的python相关期末复习总结,就是为了方便复习啦!有错的话欢迎大家留言哦

一些基础的python知识,放在这是因为印象笔记存的太多啦嘿嘿


1.Recursive functions has two cases

  • the base case, used to stop recursion
  • the recursive case
    2.有三个概念与函数式编程密切相关:映射、过滤、缩减( mapping/filtering/reducing)
***map函数***接受一个函数和一个可迭代对象作为参数,为了提高效率,它返回一个迭代器而不是一个列表
生成一个新的可迭代对象I,其中每一项都来自于原来的可迭代对象I,提供该函数在调用时返回True--***filter函数***
函数在可迭代对象的前两个值上调用,然后在计算结果上调用第三个值,然后在计算结果上调用第四个值,以此类推,直到所有的值都被使用--***reduce函数***

3.使用map()、filter()和functools.reduce()通常会导致循环的消除:标黄部分为我认为的重点
在这里插入图片描述
在这里插入图片描述
4.Mistakes in a program:

  • may prevent it from being executed F it is not possible to understand which program was written;
  • may not terminate and/or lead to the production of wrong outputs F which can actually have tremendous impacts (and actually cost lives!);
    5.三种错误:
    (1)语法错误:最快的显示,由编译器;最容易修复
    (2)逻辑错误(包含可用性错误):程序运行,但它的某些方面的行为不是预期的
    (3)性能错误:这通常是由于***算法和/或数据结构***选择不当造成的
    在这里插入图片描述
    Logical Errors are typically the most challenging to identify
    Can often be prevented by Test Driven Development (TDD)(测试驱动开发)
    5.Debugging is about locating and repairing mistakes in programs.
    6.进行定期备份是编程的一个基本部分(regular backups $ version control )
    7.版本控制系统:允许我们在任何粒度级别上增量地保存更改,并测试一些更改,如果需要的话还可以恢复到旧版本
    在这里插入图片描述
    8.(1) run a program with a syntax error print:
    the filename/line number/offending line, with a caret ^ underneath
    (2) If an unhandled exception occurs at runtime:
    Python stops execution and prints a traceback(
  • only tell us where to look for the problem,not how to solve it!
  • sometimes reveals an exception that occurred in Python’s standard library, or in a third-party library
    <Tracebacks should be read from their last line back to their first line>)
    The last line specifies the unhandled exception that occurred
    Above this line, Python shows:
    the filename, line number and function name, followed
    in a new line, by the line that caused the exception

    (如果引发异常的函数是由另一个函数调用的,则上面的元素将显示为调用函数;
    这一直到调用堆栈的开始)
    9.我们捕获并且不能恢复的异常应该以错误消息的形式报告,同样的情况也适用于具有图形界面的程序,还应以消息的形式通知用户
    10…To be able to kill a bug:
    Reproduce the bug ( we should find the smallest input and the least amount of processing that can still produce the bug)—>Locate the bug(Think of an explanation - a hypothesis - that reasonably accounts for the bug/Create an experiment to test the hypothesis/Run the experiment)—>Fix the bug (arguments coming into the function->local variables and return value->function
    < intrusive approach : inserting print() statements in the program
    non-intrusive approach :using a debugger >)—>Test the fix( testing to see if the bug it is intended to fix has gone away but also that our fix has not introduced other bugs )

=>注:找错方法
(1)print(locals(), “\n”) 函数中locals() returns a dictionary whose keys are the names of the local variables and whose values are the variables’ values
(2) pdb.set_trace() as the 1st statement of the function to examine I when the program is run, pdb stops it immediately after the pdb.set_trace() call
两个标准调试器:PDB,IDLE
在这里插入图片描述
11.unit testing:testing individual functions, classes and methods在这里插入图片描述
12.随机优化可能会导致引入bug或加速对程序整体性能没有影响的部分程序
在这里插入图片描述
The timeit.Timer.timeit() method’s return value is the time taken in seconds, as a float
13.process为资源分配的最小单位/threads为线程一定隶属于进程
14.每个程序都需要它的资源来执行,eg:变量的值/构建它的指令
15.在这里插入图片描述
(并发是由操作系统处理的,这简化了我们的工作—在进程之间共享数据,并聚合它们的结果,是困难的)
在这里插入图片描述
( 并发必须由程序员来处理,这样可以更简单地合并/处理多个线程的结果)
16.One problem with locking is the risk of deadlock:
thread #1 holds lock A and is trying to acquire lock B, while thread #2 holds lock B and is trying to acquire lock A I Both threads are blocked
17.在这里插入图片描述
Threads are started by calling the start() method, which will call run()(类中线程只能调用start()一个方法,run()通过start()调用,要用其他方法放到run()下边)
18.在线程上下文中,对数据的序列化访问( Serialized access to data )意味着
(1)确保一次只有一个线程可以访问数据
(2)不必自己分配工作
19.Once the 1st file is added, one of the threads can start working on it and so on until all the threads have a file to work on .
As soon as a thread finishes working on a file it can get another one, until all the files are processed
在这里插入图片描述
在这里插入图片描述
19.multiprocessing模块就是跨平台版本的多进程模块。
multiprocessing模块提供了一个Process类来代表一个进程对象
线程进程理解:
创建子进程时,只需要传入一个执行函数和函数的参数,创建一个Process实例,用start()方法启动
join()方法可以等待子进程结束后再继续往下运行,通常用于进程间的同步
对多个进程调用进程池的方法:对Pool对象调用join()方法会等待所有子进程执行完毕,调用join()之前必须先调用close(),调用close()之后就不能继续添加新的Process了
在这里插入图片描述
join方法 是让主进程等待子进程运行完毕后再执行主进程的。(即主进程阻塞)
启动一个线程就是把一个函数传入并创建Thread实例,然后调用start()开始执行
由于任何进程默认就会启动一个线程,我们把该线程称为主线程,主线程又可以启动新的线程,Python的threading模块有current_thread()函数,它永远返回当前线程的实例
多线程和多进程最大的不同在于,多进程中,同一个变量,各自有一份拷贝存在于每个进程中,互不影响,而多线程中,所有变量都由所有线程共享,所以,任何一个变量都可以被任何一个线程修改,因此,线程之间共享数据最大的危险在于多个线程同时改一个变量,把内容给改乱了
在这里插入图片描述
锁的好处就是确保了某段关键代码只能由一个线程从头到尾完整地执行,坏处当然也很多,首先是阻止了多线程并发执行,包含锁的某段代码实际上只能以单线程模式执行,效率就大大地下降了。其次,由于可以存在多个锁,不同的线程持有不同的锁,并试图获取对方持有的锁时,可能会造成死锁,导致多个线程全部挂起,既不能执行,也无法结束,只能靠操作系统强制终止

进程与线程的比较:
多进程:
优点:稳定性高,一个子进程崩溃了,不会影响主进程和其他子进程
缺点:创建进程的代价大,且操作系统能同时运行的进程数也是有限(有内存和CPU的限制)
多线程:
优点:多线程模式通常比多进程快一点
缺点:任何一个线程挂掉都可能直接造成整个进程崩溃,因为所有线程共享进程的内存
在Thread和Process中,应当优选Process,因为Process更稳定,而且,Process可以分布到多台机器上,而Thread最多只能分布到同一台机器的多个CPU上

20.
在这里插入图片描述

Python’s low-level socket module supports:
(1) IP addresses
(2) networking protocols:

  • user datagram protocol (UDP/用户数据报协议):轻量级但是不可靠;数据以离散数据包的形式发送,但不能保证它们会到达
  • transmission control protocol (TCP/传输控制协议):可靠的面向连接和流的协议;可以发送任何数量的数据——套接字必须将数据分解成足够小的块,以便发送,并在另一端重新构造数据
    21.请添加图片描述

请添加图片描述
=>注:Python的程序有两中退出方式:os._exit(), sys.exit()
os._exit()会直接将python程序终止,之后的所有代码都不会继续执行。
sys.exit()会引发一个异常:SystemExit,如果这个异常没有被捕获,那么python解释器将会退出。如果有捕获此异常的代码,那么这些代码还是会执行
22…网络协议补充:
Socket是网络编程的一个抽象概念。通常我们用一个Socket表示“打开了一个网络链接”,而打开一个Socket需要知道目标计算机的IP地址和端口号,再指定协议类型即可
一个Socket依赖4项:服务器地址、服务器端口、客户端地址、客户端端口来唯一确定一个Socket
请添加图片描述
请添加图片描述
服务端调用 socket 对象的 accept 方法。该方法等待客户端的连接,并返回 connection 对象,表示已连接到客户端
Sys.argv[ ]其实就是一个列表,里边的项为用户输入的参数,关键就是要明白这参数是从程序外部输入的,而非代码本身的什么地方,要想看到它的效果就应该 将程序保存了,从外部来运行程序并给出参数。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值