Modern Python Cookbook》(Python经典实例) 编写长行代码

Python一行代码的长度一般限制为不超过80个字符,因为这种做法符合众所周知的平面设计原则:短代码可读性强。关于每行的长度众说纷纭,但65个字符通常被认为是最理想的。

语法规则清楚地表明,语句必须在单个逻辑(logical)行上完成。
术语“逻辑行”提示了应该如何解决长语句问题。Python区分逻辑行和物理行,我们可以利用这些语法规则分解长语句。逻辑行包含一个完整的语句,它可以通过行连接(line joining)技术跨越多个物理行。Python语言参考手册称这种技术为显式行连接(explicit line joining)和隐式行连接(implicit line joining)。

为了提高长语句的可读性,Python提供了多种包装长语句的方法。

可以在行的结尾使用\续行。\必须是行内的最后一个字符。\之后甚至不能有空格。空格是很难察觉到的,因此不鼓励使用\。
根据Python的语法规则,语句可以跨越多个物理行,因为()、[]和{}字符必须平衡。
除了使用()和\,还可以利用Python自动连接相邻字符串字面量的方式来创建一个更长的字面量,(“a” “b”)和"ab"是一样的。
在某些情况下,可以通过将中间结果赋给单独的变量来分解语句。

Key Features Develop succinct, expressive programs in Python Learn the best practices and common idioms through carefully explained and structured recipes Discover new ways to apply Python for the new age of development Book Description Python is the preferred choice of developers, engineers, data scientists, and hobbyists everywhere. It is a great scripting language that can power your applications and provide great speed, safety, and scalability. By exposing Python as a series of simple recipes, you can gain insight into specific language features in a particular context. Having a tangible context helps make the language or standard library feature easier to understand. This book comes with over 100 recipes on the latest version of Python. The recipes will benefit everyone ranging from beginner to an expert. The book is broken down into 13 chapters that build from simple language concepts to more complex applications of the language. The recipes will touch upon all the necessary Python concepts related to data structures, OOP, functional programming, as well as statistical programming. You will get acquainted with the nuances of Python syntax and how to effectively use the advantages that it offers. You will end the book equipped with the knowledge of testing, web services, and configuration and application integration tips and tricks. The recipes take a problem-solution approach to resolve issues commonly faced by Python programmers across the globe. You will be armed with the knowledge of creating applications with flexible logging, powerful configuration, and command-line options, automated unit tests, and good documentation. What you will learn See the intricate details of the Python syntax and how to use it to your advantage Improve your code readability through functions in Python Manipulate data effectively using built-in data structures Get acquainted with advanced programming techniques in Python Equip yourself with functional and statistical programming features Write proper tests to be sure a program works as advertised Integrate application software using Python Table of Contents Chapter 1. Numbers, Strings, and Tuples Chapter 2. Statements and Syntax Chapter 3. Function Definitions Chapter 4. Built-in Data Structures – list, set, dict Chapter 5. User Inputs and Outputs Chapter 6. Basics of Classes and Objects Chapter 7. More Advanced Class Design Chapter 8. Functional and Reactive Programming Features Chapter 9. Input/Output, Physical Format, and Logical Layout Chapter 10. Statistical Programming and Linear Regression Chapter 11. Testing Chapter 12. Web Services Chapter 13. Application Integration
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值