《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

Beginning Python From Novice to Professional(3rd) epub

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

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 790

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

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

Unity中实现HLS视频加密与流式播放的完整方案

本文详细介绍了在Unity中实现HLS视频加密与流式播放的完整方案。针对Unity原生VideoPlayer无法直接支持实时解密的问题,提出了基于HLS协议与AES-128加密的成熟解决方案,并重点阐述了如何利用AVPro Video插件进行集成。文章涵盖了从密钥生成、使用FFmpeg进行视频加密切片,到在Unity中动态加载密钥并播放加密流的全流程,为保护付费课程、独家动画等视频内容提供了安全且高效的实践路径。

weixin_29290963的博客 397

《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

Cadence IC618/Spectre231安装避坑指南:详解License配置、环境变量隔离与依赖检查

本文详细介绍了Cadence IC618/Spectre231的安装与配置避坑指南,包括许可证系统精妙控制、环境隔离模块化实践、依赖管理进阶技巧以及长期维护的黄金法则。通过实战案例和解决方案,帮助工程师构建稳定可靠的EDA工作环境,提升芯片设计效率。

weixin_28683065的博客 476

《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

从零到一:基于PX4+Gazebo+ROS2的无人机轨迹跟踪仿真环境全栈搭建

本文详细介绍了在Ubuntu 22.04系统上,从零开始搭建基于PX4、Gazebo Garden和ROS2 Humble的无人机轨迹跟踪仿真环境的完整流程。内容涵盖PX4 SITL编译、Gazebo安装、ROS2配置、Micro XRCE-DDS Agent通信桥接以及QGroundControl地面站部署,并提供了创建自定义ROS2节点实现圆形轨迹跟踪的实战代码与关键调试技巧,帮助开发者快速构建全栈仿真开发环境。

swift5iosmith的博客 933

手把手教你用Ubuntu Desktop搭建个人L2TP服务器,手机电脑都能连(Network Manager配置详解)

本文详细介绍了在Ubuntu Desktop上使用Network Manager图形化配置L2TP服务器的方法,实现手机、电脑等多设备安全连接。从环境准备、图形界面设置到各平台客户端配置,提供一站式解决方案,并包含常见问题排查与安全增强措施,帮助用户快速搭建个人远程访问网络。

weixin_30555125的博客 129

《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专栏 894

如何实现MCGS与S7-200SMART PLC以太网多台通信控制?

第九步:(设备工具箱—设备管理—鼠标依次点击通用TCP/IP父设备—西门子_ Smart200到设备窗口里面去)数据类型:第0位(指PLC地址变量小数点后面的位数,例如我地址是M0.0,那么在数据类型应填写通道的第00位)第十步:(鼠标双击打开通用TCPIP父设备0--[通用TCP/IP父设备]进入通信配置)通道地址:0(指小数点前面起始字节的地址,例如我地址是M0.0,那么应在通道地填写0)。第七步:(选择通用TCP/IP父设备,点击新增,右侧选定设备就会自动添加进去。

u010670857的博客 6731

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

……

u013669912的博客 1165

代码实现ROS2bag录制数据和回放数据(C++)

ROS2的ROS2bag功能可以将指定Topic中的数据以.db3的格式(不同于ROS)保存到本地文件中。读取文件数据可以实现数据回放。

m0_57124501的博客 3262

华为od面试手撕真题【分解质因数】

华为od面试手撕真题【分解质因数】

MISAYAONE的博客 1157

《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

Vue+OpenLayers:解决中国境内OpenStreetMap地图加载问题

然而,在使用OpenLayers和Vue框架搭建地图应用时,很多中国开发者可能会遇到一个常见问题:OpenStreetMap(OSM)地图无法正常加载。为了解决这个问题,我们可以使用其他方式获取地图数据,比如使用国内的地图服务提供商。通过使用国内地图服务商提供的地图数据,我们可以解决OpenStreetMap地图加载问题。然后,我们创建一个OpenLayers地图对象,并指定地图图层的源为国内地图服务商提供的地图URL。属性,可以将地图的中心点和缩放级别调整到我们需要的位置。

CodeWhizZ的博客 8285

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

NTscan扫描工具

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

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

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

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

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 1380

Beginning Python From Novice to Professional (7) - 类

类 创建简单类: #!/usr/bin/env python __metaclass__ = type class Person: def setName(self,name): self.name = name def getName(self): return self.name def greet(self): print "Hello,world! I'm %s."

Marvin_wu 942

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

n大魔力1.构造函数class FooBar: def __init__(self, value=42): self.somevar = value   f = FooBar(This is a constructor argument)f.somevar   --->This is a constructor argument在继承机制中调用父类的构造函数

thy专栏 1006
上一篇: 《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、付费专栏及课程。

余额充值
⌂ 换网址