没有合适的资源?快使用搜索试试~ 我知道了~
首页Dive Into Python 3 无水印pdf
资源详情
资源评论
资源推荐
CHAPTER -1. WHAT’S NEW IN “DIVE INTO PYTHON
3”
❝ Isn’t this where we came in? ❞
— Pink Floyd, The Wall
-1.1. A.K.A. “THE MINUS LEVEL”
You read the original “Dive Into Python” and maybe even bought it on paper. (Thanks!) You already know
Python 2 pretty well. You’re ready to take the plunge into Python 3. … If all of that is true, read on. (If none of
that is true, you’d be better off starting at the beginning.)
Python 3 comes with a script called 2to3. Learn it. Love it. Use it. Porting Code to Python 3 with 2to3 is a
reference of all the things that the 2to3 tool can fix automatically. Since a lot of those things are syntax changes,
it’s a good starting point to learn about a lot of the syntax changes in Python 3. (print is now a function, `x`
doesn’t work, &c.)
Case Study: Porting chardet to Python 3 documents my (ultimately successful) effort to port a non-trivial library
from Python 2 to Python 3. It may help you; it may not. There’s a fairly steep learning curve, since you need to
kind of understand the library first, so you can understand why it broke and how I fixed it. A lot of the breakage
centers around strings. Speaking of which…
Strings. Whew. Where to start. Python 2 had “strings” and “Unicode strings.” Python 3 has “bytes” and
“strings.” That is, all strings are now Unicode strings, and if you want to deal with a bag of bytes, you use the
new bytes type. Python 3 will never implicitly convert between strings and bytes, so if you’re not sure which one
you have at any given moment, your code will almost certainly break. Read the Strings chapter for more details.
Bytes vs. strings comes up again and again throughout the book.
1
• In Files, you’ll learn the difference between reading files in “binary” and “text” mode. Reading (and writing!) files
in text mode requires an encoding parameter. Some text file methods count characters, but other methods
count bytes. If your code assumes that one character == one byte, it will break on multi-byte characters.
• In HTTP Web Services, the httplib2 module fetches headers and data over HTTP. HTTP headers are returned
as strings, but the HTTP body is returned as bytes.
• In Serializing Python Objects, you’ll learn why the pickle module in Python 3 defines a new data format that is
backwardly incompatible with Python 2. (Hint: it’s because of bytes and strings.) Also JSON, which doesn’t
support the bytes type at all. I’ll show you how to hack around that.
• In Case study: porting chardet to Python 3, it’s just a bloody mess of bytes and strings everywhere.
Even if you don’t care about Unicode (oh but you will), you’ll want to read about string formatting in Python 3,
which is completely different from Python 2.
Iterators are everywhere in Python 3, and I understand them a lot better than I did five years ago when I wrote
“Dive Into Python”. You need to understand them too, because lots of functions that used to return lists in
Python 2 will now return iterators in Python 3. At a minimum, you should read the second half of the Iterators
chapter and the second half of the Advanced Iterators chapter.
By popular request, I’ve added an appendix on Special Method Names, which is kind of like the Python docs
“Data Model” chapter but with more snark.
When I was writing “Dive Into Python”, all of the available XML libraries sucked. Then Fredrik Lundh wrote
ElementTree, which doesn’t suck at all. The Python gods wisely incorporated ElementTree into the standard
library, and now it forms the basis for my new XML chapter. The old ways of parsing XML are still around, but
you should avoid them, because they suck!
Also new in Python — not in the language but in the community — is the emergence of code repositories like
The Python Package Index (PyPI). Python comes with utilities to package your code in standard formats and
distribute those packages on PyPI. Read Packaging Python Libraries for details.
2
CHAPTER 0. INSTALLING PYTHON
❝ Tempora mutantur nos et mutamur in illis. (Times change, and we change with them.) ❞
— ancient Roman proverb
0.1. DIVING IN
Welcome to Python 3. Let's dive in. In this chapter, you'll install the version of Python 3 that's right for
you.
0.2. WHICH PYTHON IS RIGHT FOR YOU?
The first thing you need to do with Python is install it. Or do you?
If you're using an account on a hosted server, your ISP may have already installed Python 3. If you’re running
Linux at home, you may already have Python 3, too. Most popular GNU/Linux distributions come with Python 2
in the default installation; a small but growing number of distributions also include Python 3. (As you’ll see in this
chapter, you can have more than one version of Python installed on your computer.) Mac OS X includes a
command-line version of Python 2, but as of this writing it does not include Python 3. Microsoft Windows does
not come with any version of Python. But don’t despair! You can point-and-click your way through installing
Python, regardless of what operating system you have.
The easiest way to check for Python 3 on your Linux or Mac OS X system is to get to a command line. On
Linux, look in your Applications menu for a program called Terminal. (It may be in a submenu like Accessories
or System.) On Mac OS X, there is an application called Terminal.app in your /Application/Utilities/ folder.
Once you’re at a command line prompt, just type python3 (all lowercase, no spaces) and see what happens. On
my home Linux system, Python 3 is already installed, and this command gets me into the Python interactive shell.
3
mark@atlantis:~$ python3
Python 3.0.1+ (r301:69556, Apr 15 2009, 17:25:52)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
(Type exit() and press ENTER to exit the Python interactive shell.)
My web hosting provider also runs Linux and provides command-line access, but my server does not have
Python 3 installed. (Boo!)
mark@manganese:~$ python3
bash: python3: command not found
So back to the question that started this section, “Which Python is right for you?” Whichever one runs on the
computer you already have.
[Read on for Windows instructions, or skip to Installing on Mac OS X, Installing on Ubuntu Linux, or Installing
on Other Platforms.]
⁂
0.3. INSTALLING ON MICROSOFT WINDOWS
Windows comes in two architectures these days: 32-bit and 64-bit. Of course, there are lots of different versions
of Windows — XP, Vista, Windows 7 — but Python runs on all of them. The more important distinction is
32-bit v. 64-bit. If you have no idea what architecture you’re running, it’s probably 32-bit.
Visit python.org/download/ and download the appropriate Python 3 Windows installer for your architecture.
Your choices will look something like this:
• Python 3.1 Windows installer (Windows binary — does not include source)
4
• Python 3.1 Windows AMD64 installer (Windows AMD64 binary — does not include source)
I don’t want to include direct download links here, because minor updates of Python happen all the time and I
don’t want to be responsible for you missing important updates. You should always install the most recent
version of Python 3.x unless you have some esoteric reason not to.
Once your download is complete, double-
click the .msi file. Windows will pop up a
security alert, since you’re about to be
running executable code. The official Python
installer is digitally signed by the Python
Software Foundation, the non-profit
corporation that oversees Python
development. Don’t accept imitations!
Click the Run button to launch the Python 3
installer.
5
剩余472页未读,继续阅读
yinkaisheng-nj
- 粉丝: 763
- 资源: 6953
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
- SPC统计方法基础知识.pptx
- MW全能培训汽轮机调节保安系统PPT教学课件.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0