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

全面掌握Python:从初学者到专业开发者的进阶之路(第3版) 本文还有配套的精品资源,点击获取 简介:《初识Python:从新手到专业(第3版)》是一本适合不同经验层次程序员的Python编程指南,涵盖了从基础知识到高级概念的完整学习路径。本书内容包括Python基础语法、面向对象编程、函数式编程、文件操作、网络编程、数据库集成、Web开发以及自动化任务等。同时,它还介绍了测试、调试、版本控制等软件开发的实践技能,以及大数据分析、机器... 阅读详情
1.属性访问

这种技术语法上不错,但它将程序与一种accessor紧耦合了。
函数property能够解决这个问题:

原理:其实property是个满足descriptor协议的类(这样的类实现了__get__, __set__和__delete__方法)
下面测试accessor:
r = Rectangle()
r.width = 10
r.height = 5
r.size   ---> (10, 5)
r.size = 150, 100
r.width   ---> 150

实际上property函数也可以使用0个、1个、3个、4个参数。
0个 —— resulting property 既不可读也不可写
1个 —— resulting property 仅仅可读
3个 —— 第3个参数是用来删除某个属性的方法(此方法无参数)
4个 —— 第4个参数是documentation string
以上4个参数的名称为 fget, fset, fdel以及doc。
property(fget, fset, fdel, doc)

作者最后说,The moral is this: With new-style classes, you should use property rather than accessors.

2. 静态方法和类方法
静态方法是没有self参数的方法,而且可以由类直接调用。
类方法是使用了一个类似于self的参数cls。

以下是一个小例子:

在Python 2.4中, 引入了一个新的语法:decorators
   
以下为调用:
MyClass.smeth()
---> This is a static method
MyClass.cmeth()
---> This is a class method of <class '__main__.MyClass'>
静态方法和类方法之所以在Python中不太重要,是因为它未能在早期版本中实现。但在像factory函数中,它还是非常有用的。

3.__getattr__, __setattr__, and Friends
__getattribute__(self, name): 当属性name被访问时,被自动调用(Works correctly on new-style classes only.)   #Note:TODO
__getattr__(self, name):当属性name被访问时,被自动调用
__setattr__(self, name, value):当属性name要被赋值value时,被自动调用
__delattr__(self, name): 当属性name要被删除时,被自动调用
这些magic方法在处理多个属性的时候是很有用的,但是If you have a choice, though, stick with property.
一个使用例子:
   
4.迭代器Iterators
实现了__iter__方法的对象就可以和Sequences和Dictionaries一样在for循环中迭代。
__iter__方法返回一个迭代器,这是一个拥有next方法的任意对象,可调用但无参数。如果调用迭代器时已经没有了返回值,但会引发一个StopIteration异常。

!下面是一个可迭代的类示例:

下面在for循环中使用这个迭代:
#内置函数iter可以从一个可迭代对象中获得迭代器iter(obj)            Extracts an iterator from an iterable object.

除了使用迭代器遍历对象,还可以将其转换成Sequence:
...
ti = TestIterator()
list(ti)   ---> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

5.Generators(本质也是一种迭代)
#书中后面给出了定义:A generator is a function that contains the keyword yield. When called, the generator function returns a generator, which is a special type of iterator.

Making a Generator:

或者
list(flatten(nested))
---> [1, 2, 3, 4, 5]

递归Generator:(可以用来表示某种树结构)
   
更简洁的调用方式:
list(flatten([[[1],2],3,4,[5,[6,7]],8]))
---> [1, 2, 3, 4, 5, 6, 7, 8]

以上递归方式有一个隐患,当迭代string-like对象时,因为它是一个sequence,所以不会引起TypeError异常。(不过不应该迭代String-like对象),下面是一种更为安全的定义方式:
   
使用示例:
list(flatten(['foo', ['bar', ['baz']]]))
---> ['foo', 'bar', 'baz']
generators包含两总分:generator函数和generator迭代器。generator函数是包含yield的函数; generator迭代器是generator函数的返回值,和其它迭代器一样使用。

def simple_generator():
        yield 1
...
simple_generator
---> <function simple_generator at 153b44>   #这里被看成一个函数
simple_generator()
---> <generator object at 1510b0>   #加括号则看成一个generator
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! 立即下载

相关推荐

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》学习笔记六: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

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

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

这些年我读过的技术经典图书(附电子版下载地址)

本人大学期间专业是数学与应用数学, 2009年毕业, 学习不怎么样. 毕业后才开始接触计算机, 做过C开发, 系统运维和测试, 这4年我读了很多书, 电子书和纸质的, 这些书中有很多很经典的图书和资料, 现在拿来和大家分享一下. 每个资料都有下载衔接,大家可以直接下载, 都是免费的. C技术资料 1.> 作者: 谭浩强 这是我推荐的第一本书, 也是我接触的第一本书, 为什么把它

shitao827194819的专栏 2620

Python基础教程 Beginning Python From Novice to Professional.zip

Python基础教程 Beginning Python From Novice to Professional.zip,收录了 Python基础教程 Beginning Python From Novice to Professional 中英文各版本和源码,包括(第2版)和 (第3版),都有详尽的书签,不是一级目录的,有的电子版还没有。

python基础教程(Beginning Python From Novice to Professional)书中源码例子

python基础教程 (Beginning Python From Novice to Professional) 书中源码例子

《Python基础教程(第三版)》阅读笔记

本文参考自《Beginning Python: from novice to professional》,中文版为《Python基础教程(第三版)》。

yyywxk的博客 612

Beginning Python From Novice To Professional(2rd Edition) (中文版)

Python开发教程,包含基本语法的说明及大量实例讲解,适合初级开发人员学习,可作为高级开发人员参考资料

《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 英文第3版

Gain a fundamental understanding of Python’s syntax and features with this 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 Python 3, Beginning Python also covers advanced topics such as extending Python and packaging/distributing Python applications. 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 Who This Book Is For Programmers, novice and otherwise, seeking a comprehensive introduction to the Python programming language.

Beginning Python From Novice to Professional Second Edition

Beginning Python From Novice to Professional Second Edition

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

……

u013669912的博客 1165

别再乱选WiFi信道了!手把手教你用WiFi Analyzer优化2.4G/5G家庭网络(附避坑指南)

本文详细介绍了如何使用WiFi Analyzer工具优化家庭WiFi网络,解决2.4GHz和5GHz信道的冲突问题。通过分析信道拥堵原因、提供安装使用指南及优化策略,帮助用户显著提升网络速度与稳定性,特别适合密集住宅区和多设备家庭。

weixin_42665725的博客 5898

Python基础教程

本书包括Python程序设计的方方面面,首先从Python的安装开始,随后介绍了Python的基础知识和基本概念,包括列表、元组、字符串、字典以及各种语句。然后循序渐进地介绍了一些相对高级的主题,包括抽象、异常、魔法方法、属性、迭代器。此后探讨了如何将Python与数据库、网络、C语言等工具结合使用,从而发挥出Python的强大功能,同时介绍了Python程序测试、打包、发布等知识。最后,作者结合

xiaotom5的专栏 1324

湿敏电阻HR202的两种驱动方案实测:IO充放电法 vs. 交流方波ADC法,哪个更适合你?

本文深入评测湿敏电阻HR202的两种驱动方案:IO充放电法和交流方波ADC法。通过原理图分析、精度实测和资源占用对比,揭示交流方波ADC法在精度和稳定性上的优势,特别适合长期监测场景。同时提供IO充放电法在资源受限情况下的应用建议,帮助工程师根据项目需求做出最优选择。

weixin_30421809的博客 471

使用mne包来对脑电数据进行预处理(一)

MNE(MNE-Python)是一个用于处理脑电(EEG)、磁图(MEG)和其他神经科学数据的Python库。它提供了丰富的功能,包括数据加载、预处理、分析和可视化,旨在帮助研究人员和神经科学家更轻松地进行脑电数据的处理和分析。在本文中,我们使用mne来对不同的格式的脑电数据集进行数据读取。

m0_71596228的博客 4314

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

RTX 50系显卡(5080/5090D)CUDA 12.8环境下的vLLM部署避坑与实战指南

本文为RTX 5080/5090D显卡用户提供了在CUDA 12.8环境下部署vLLM的完整实战指南。文章详细介绍了从驱动安装、CUDA 12.8环境配置、PyTorch Nightly版本精准匹配,到vLLM源码编译及常见报错排查的全过程,帮助开发者高效利用新一代硬件进行大模型推理。

gray5的博客 675

《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

AI工程化之生成式UI & A2UI(一)

本文系统介绍了生成式UI和Google A2UI技术。生成式UI通过AI直接生成前端代码(完全生成式)或DSL(混合式),实现千人千面的动态界面和研发提效。Google A2UI提出组件级和消息级协议,采用JSON规范实现渲染框架解耦,支持多端渲染。文章详细阐述了A2UI的架构设计,包括协议生成、服务端处理和渲染器实现,并对比了与传统前端框架的差异,指出A2UI本质是UI=F(AI(UI)+AI(state))的创新模式。最后通过playground项目展示了A2UI的实践应用。

qq_34306228的博客 597

C++怎么学?零基础也可以,一份完整学习路线图助你搞定!

提供一条清晰、系统且实践导向的C++学习路径。从基础语法、面向对象、泛型编程、异常处理,逐步深入STL、智能指针、并发编程等现代C++高级特性。无论是新手还是希望提升,通过系统学习与项目实践,助您将理论知识转化为实际解决问题的能力。

Lion_Long的博客 4051

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

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.创建函数 def fibs(num): result = [0, 1] for i in range(num-2)

thy专栏 851

YOLOv5源码逐行超详细注释与解读(3)——训练部分train.py

全网最详细的YOLOv5项目源码解读之训练部分train. py。全文近5万字!代码逐行注释,逐段讲解,小白入门必备!

路人贾的博客 3万+

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

从统一入口到运营中枢:全面看懂 SAP for Me 的能力边界、使用方式与项目价值

SAP for Me 是 SAP 推出的统一工作平台,旨在整合原本分散在多个系统中的关键信息与操作。它将许可证管理、系统状态、支持工单、维护合同、产品路线图等核心功能集中到一个界面,为不同角色提供定制化视图。平台包含多个功能模块:Home 作为个性化工作台展示待办事项与风险;Calendar 整合各类维护事件与关键时间节点;Reporting 提供跨域分析与趋势洞察;Finance & Legal 管理订单、发票与合规事项;Portfolio & Products 则展示产品全景与路线图。该

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 132

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

上一篇: 《Beginning Python From Novice to Professional》学习笔记十一:__Magic__
下一篇: Effective C++ 3nd 读书摘要(一、让自己习惯C++ ; 二、构造,析构,赋值运算)Item1 - 12
thy38
博客等级 码龄19年 184粉丝 116原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址