《Beginning Python From Novice to Professional》学习笔记八:Abstraction

Beginning Python: From Novice to Professional 第二版 Gain a fundamental understanding of Python’s syntax and features with the second edition of Beginning Python, an up–to–date introduction and practical reference. Covering a wide array of Python–related programming topics, including addressing language internals, database integration, network programming, and web services, you’ll be guided by sound development principles. Ten accompanying projects will ensure you can get your hands dirty in no time. Updated to reflect the latest in Python programming paradigms and several of the most crucial features found in the forthcoming Python 3.0 (otherwise known as Python 3000), advanced topics, such as extending Python and packaging/distributing Python applications, are also covered. What you’ll learn * Become a proficient Python programmer by following along with a friendly, practical guide to the language’s key features. * Write code faster by learning how to take advantage of advanced features such as magic methods, exceptions, and abstraction. * Gain insight into modern Python programming paradigms including testing, documentation, packaging, and distribution. * Learn by following along with ten interesting projects, including a P2P file–sharing application, chat client, video game, remote text editor, and more. Complete, downloadable code is provided for each project! Who is this book for? Programmers, novice and otherwise, seeking a comprehensive introduction to the Python programming language. About the Apress Beginning Series The Beginning series from Apress is the right choice to get the information you need to land that crucial entry–level job. These books will teach you a standard and important technology from the ground up because they are explicitly designed to take you from “novice to professional.” You’ll start your journey by seeing what you need to know—but without needless theory and filler. You’ll build your skill set by learning how to put together real–world projects step by step. So whether your goal is your next career challenge or a new learning opportunity, the Beginning series from Apress will take you there—it is your trusted guide through unfamiliar territory! 立即下载

Laziness Is a Virtue --- Real programmers are lazy. Not lazy in a bad way, but in the sense that they don’t do unnecessary work.

1.创建函数

fibs(10) ---> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

2.Documenting Functions - 方便他人理解
可以用#加以注释,但也可以在函数名后直接加上String,即可成为函数的一部分(docstring),可以用__doc__访问(此机制有点类似于Matlab)


square.__doc__
---> 'Calculates the square of the number x.'
help (square)
---> Help on function square in module __main__:
---> square(x)
        Calculates the square of the number x.

3.参数的可更改性
不可更改的(immutable)元素,如Strings、numbers和tuples作为参数传入时,在函数内部改变其形参值不会影响实参的值。

try_to_change(name)
name   ---> 'Mrs. Entity'

但是可更改的(mutable)元素,如List、Dictionary

change(names)
names   ---> ['Mr. Gumby', 'Mrs. Thing']

如果想让List元素传入而不受函数内部的影响,可以做一份拷贝,接上例
change(names[:])   #name[:]会返回一份name的拷贝
names   ---> ['Mrs. Entity', 'Mrs. Thing']
最后,如果参数是不可更改的,则函数内部的更改无法(Sorry, but it can’t be done.)解决方案有二:
一、将在内部更改过的参数全部返回(超过一个的话可以用Tuple)
def inc(x): return x + 1
foo = 10
foo = inc(foo)
foo   ---> 11
二、将参数打包在一个List中作为参数传入
def inc(x): x[0] = x[0] + 1
foo = [10]
inc(foo)
foo   ---> [11]

4.带关键字的参数(Keyword Parameters)和参数默认值
前面所述的参数均为位置性参数(positional parameters),即参数的顺序位置是重要的。
通过在传入参数时带上参数的关键字可以忽略这种顺序位置(但尽量不要这样写)

hello_4('Mars', punctuation='.')  
#'Mars'没有带上关键字name,但因为它在第一个,因此被认为是传给name的
---> Hello, Mars.

5.参数Collection(参数个数的不确定化)
如下的例子很好地说明了这一概念:

但是此时参数不可以使用Keyword了:
print_params_2('Hmm...', something=42)   #解释器会将会报错unexpected keyword argument 'something'

若想使用Keyboard,可以使用以下版本:
def print_params_3(**params):   #此时参数前加了两个*,说明这之后的参数将被看成一个Dictionary
    print params

print_params_3(name='thy', age=30)   #但此时参数不可以像Dictionary一样用number作key
---> {'age': 30, 'name': 'thy'}
最后看一个综合示例:


print_params_4(1, 2, 3, 5, 6, 7, foo=1, bar=2)
---> 1 2 3 (5, 6, 7) {'foo': 1, 'bar': 2}
print_params_4(1, 2)
---> 1 2 3 () {}

6.参数解包(5的逆过程)
Tuple的解包:
def add(x, y): return x + y
params = (1, 2)
add(*params)   ---> 3

Dictionary的解包:(接5中的print_params_3函数)
params = {'name': 'Sir Robin', 'greeting': 'Well met'}
print_params_3(**params)
---> {'name': 'Sir Robin', 'greeting': 'Well met'}

两段对比函数代码:


7.作用域Scoping
内置函数vars()可返回作用域,其结构为Dictionary。
x = 1
scope = vars()   #scope为Dictionary,其中包含'x':1这一项
scope['x']   ---> 1
scope['x'] += 1   #仅作为示例,实际中不建议使用
x   ---> 2

除了全局作用域之外,每一个函数均会构建自己的一个作用域。
在函数中可以访问全局变量,但不可以更改全局变量
def combine(parameter):
    external = 'mark'
    print parameter + external

external = 'berry'
combine('Shrub')
---> Shrubmark
external
---> 'berry'   #external的全局值未受影响
作者的提醒:■Caution Referencing global variables like this is a source of many bugs. Use global variables with care.


如果在某个作用域中有变量与全局的某变量重名,可以用全局引用的方法,而且此时可以更改全局变量

以下代码可实现同样的效果:

《Python技术参考大全》:LGB规则,名字引用搜寻顺序为Local->Global->Built-in。

8.递归(书上都是说的递归的原理,也没讲什么新的内容,直接贴一点关键内容吧)
A useful recursive function usually consists of the following parts:
A base case (for the smallest possible problem) when the function returns a value directly
A recursive case, which contains one or more recursive calls on smaller parts of the problem
再来个最经典的阶乘示例:

更经典的二分查找示例(^_^)

#测试
seq = [34, 67, 8, 123, 4, 100, 95]
seq.sort()   #bisect模块中有标准的二分查找法实现
search(seq, 34)   ---> 2

 

Beginning Python From Novice to Professional(3rd) 无水印pdf Beginning Python From Novice to Professional(3rd) 英文无水印pdf 第3版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除 立即下载

相关推荐

Beginning Python: From Novice to Professional, 2nd Edition

《Python基础教程, 第三版》的英文电子版。本书是经典的Python入门教程,层次鲜明、结构严谨、内容翔实,特别是后面几章,作者将前面讲述的内容应用到10个引人入胜的项目中,并以模板的形式介绍了项目的开发过程,手把手教授Python编程实践,让读者从项目中领略Python的真正魅力。

《Beginning Python From Novice to Professional》学习笔记五:Advanced String

1.格式化 format=hello, %s. Your ID is %d. values=(thy, 38) print format % values ---> hello, thy. Your ID is 38. 如第二句如示,要修饰多个元素,可以用Tuple,也可以用Dictionary(但不可以用List,因为List会被认为仅仅是一个单值) 2.模板(此功能不是内置的,因此

thy专栏 762

Apress.Beginning.Python.From.Novice.to.Professional.3rd.Edition.

亲测很好,很新,很全的Python 入门学习资料(转点奶粉钱)

Beginning Python: From Novice to Professional

Beginning Python: From Novice to Professional is the most comprehensive book on the Python ever written. Based on Practical Python, this newly revised book is both an introduction and practical refere

TopMVP 788

Beginning Python From Novice to Professional(3rd) epub

Beginning Python From Novice to Professional(3rd) 英文epub 第3版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除

Beginning Python(3rd) 无水印pdf 0分

Beginning Python(3rd) 英文无水印pdf 2017第3版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开

《Beginning Python From Novice to Professional》学习笔记七:Statement

1.More about Print print Age:, 42 ---> Age: 42 注意以下语句的区别: 1, 2, 3 ---> (1, 2, 3) print 1, 2, 3 ---> 1 2 3 print (1, 2, 3) ---> (1, 2, 3) 2.More about Import import somemodule from somemodule import

thy专栏 1248

Android自动化新方案:ESP32-C3蓝牙HID模拟触控与手势操作实战(附源码)

本文详细介绍了使用ESP32-C3通过蓝牙HID协议实现Android设备自动化操作的完整方案。该硬件方案成本低廉且能完美规避系统风控,可模拟鼠标移动、点击及复杂手势操作,延迟低于50毫秒,适用于游戏连招和应用测试等场景。文章包含完整的源码实现和性能优化技巧。

kotlin6android的博客 378

《Beginning Python From Novice to Professional》学习笔记六:Dictionary

1.创建(注意Dictionary是没有顺序的) phonebook = {Alice: 2341, Beth: 9102, Cecil: 3258} 冒号之前为key,之后为value,key必须具有唯一性。 items = [(name, Gumby), (age, 42)] d = dict(items) ---> {age: 42, name:

thy专栏 973

从零到一:AIC8800 USB-WiFi模块在Linux内核6.x下的驱动适配与避坑指南

本文详细介绍了AIC8800 USB-WiFi模块在Linux内核6.x下的驱动适配全流程,包括环境准备、源码编译、内核API变更处理及常见问题解决方案。重点针对驱动适配中的符号导出、设备ID识别和固件加载等核心挑战提供实用指南,帮助开发者高效完成硬件驱动兼容性调试。

k8l9m0n的博客 1028

手把手教你在VirtualBox里复活Win98,并搞定驱动和文件传输(含资源寻找攻略)

本文详细指导如何在VirtualBox中安装和配置Windows 98,包括驱动安装、文件传输等实用技巧。通过逐步教程和资源寻找攻略,帮助技术爱好者完美复刻这一经典操作系统,满足怀旧或兼容性测试需求。

weixin_29596051的博客 327

《Beginning Python From Novice to Professional》学习笔记九:More Abstraction

0.类的私有性 Python中对象的属性是可以从外部访问的,本身不直接支持私有性,对此的解释是 Isn’t it enough that each object manages its own attributes? 在Python中如果要使方法或属性从外部不可访问,则必须在名字前加上两个下划线 class Secretive: def __inaccessible(self):

thy专栏 892

系统动力学仿真软件Vensim下载

Vensim是由美国Ventana Systems, Inc.所开发,是采用图示化编程的方式构建系统动力学模型,上手简单。Vensim有很多系列产品,其中Vensim PLE (Personal Learning Edition)是面向个人学习者的,完全免费,可直接官网下载,截止于2022/04/10,最新的Vensim PLE版本是9.2.3。

PMIIᶜᶜ的博客 1万+

Linux | Bash Shebang 语法与结构及常见问题排查

……

u013669912的博客 1161

Exposure Fusion

MertensTMO原理详解: 文章:2007年Exposure Fusion 论文链接:http://ntp-0.cs.ucl.ac.uk/staff/j.kautz/publications/exposure_fusion.pdf 论文代码:C版本:https://github.com/JawThrow/ExposureFusion python版本:https://github.com/lzocro/Exposure-Fusion matlab版本:https://github.com/Mericam

大长腿不解释 1653

Linux安装MinIO(图文解说详细版)

最近开个新坑,就是在linux环境中操作开发环境,带大家玩转Linux,会整理出一篇Linux的专栏,欢迎大家订阅!! 富贵同学linux环境为CentOS7.8 版本。 上期跟大家说了一下OpenJDK的安装,这次说一下minio的安装 第一步,进入/opt 目录,创建minio文件夹 cd /opt mkdir minio 第二步,wget下载安装包: wget https://dl.minio.io/server/minio/release/linux-amd64/minio 第三步,进入m

编码人生 5万+

《Beginning Python From Novice to Professional》学习笔记十二:__Magic__

1.属性访问class Rectangle: def __init__(self): self.width = 0 self.height = 0 def setSize(self, size): self.width, self.height = size #我的理解是自动组成Tuple def getS

thy专栏 970

第一讲:halcon模板匹配捆绑测量矩形检测芯片针脚

使用halcon模板匹配捆绑测量矩形,检测位置发生变换的芯片针脚缺陷。

uiopmn1的博客 2995

python3入门教程 pdf_Python基础教程第3版(Beginning Python From Novice to Professional 3th) pdf...

Magnus Lie Hetland又更新他的 "Beginning Python From Novice to Professional"了,该书为第三版英文,基于Python3.5进行讲解,紧跟技术潮流。希望对Python爱好者有所帮助。这是Python最新的介绍和实际参考。涵盖广泛的Python相关编程主题,包括语言处理、数据库集成、网络编程和Web服务,遵循良好的开发原则。十个配套项目可以...

weixin_39640911的博客 621

基于Simulink的电机功率因数校正仿真

PF=SP​=视在功率有功功率​=cosϕ⋅Irms​I1​​(非正弦时)位移因数(cosϕ):基波电压电流相位差畸变因数(I1​/Irms​):谐波导致电流有效值增大本文带你完成了电机系统功率因数校正的Simulink仿真,实现了:✅ 理解了低功率因数的成因与危害✅ 搭建了无PFC、无源PFC、有源PFC三种对比模型✅ 掌握了Vienna整流器建模与dq解耦控制方法✅ 实现了PF与THD的自动计算与评估✅ 验证了有源PFC在PF、THD、稳定性上的全面优势。

amy_mhd的博客 154

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Beginning Python From Novice to Professional (5) - 条件与循环

条件与循环 条件运行: name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr.Gumby'What is your name? Gumby Hello, Mr.Gumbyname = raw_input('What is your name? ') if name.e...

weixin_34112030的博客 179

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Beginning Python From Novice to Professional (3) - 列表操作

列表操作 list函数: >>> list('hello') ['h', 'e', 'l', 'l', 'o']改变列表: >>> x=[1,1,1] >>> x[1]=2 >>> x [1, 2, 1]删除元素: >>> names = ['wu','li','zhao','qian'] >>> del names[1] >>> names ['wu', 'zhao', 'qian']分

Marvin_wu 1203

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

Beginning Python From Novice to Professional (1) - 数字和表达式

数字和表达式 加减乘除: >>> 2+2 4 >>> 100-50 50 >>> 3*5 15 >>> 1/2 0 >>> 1.0/2.0 0.5求余、乘方: >>> 1%2 1 >>> 10%3 1 >>> 2**3 8 >>> -3**2 -9 >>> (-3)**2 9十六进制、八进制: >>> 0xff 255 >>> 020 16变量: >>> x=3 >>> x*2 6获取输

Marvin_wu 1379

Beginning Python From Novice to Professional (6) - 函数使用

函数使用 定义函数:

Marvin_wu 970
上一篇: 《Beginning Python From Novice to Professional》学习笔记七:Statement
下一篇: More Effective C++ 读书摘要(五、技巧2)Item28 - 29
thy38
博客等级 码龄19年 184粉丝 116原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值