Você está na página 1de 495

CHAPTER -1.

WHATS NEW IN DIVE INTO PYTHON


3
Isnt this where we came in?
Pink Floyd, The Wall

-1.1. A.K.A. THE MINUS LEVEL

&

never

will

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

B
0.2. WHICH PYTHON IS RIGHT FOR YOU?

Python interactive shell

0.3. INSTALLING ON MICROSOFT WINDOWS

versions

Python 3.1 Windows installer


Python 3.1 Windows AMD64 installer

Register Extensions

Tcl/Tk
Documentation
Utility Scripts

Test Suite

0.4. INSTALLING ON MAC OS X

Python 3.1
Mac Installer Disk Image

must

Python

Framework
GUI Applications
UNIX command-line tools
Python Documentation
Shell profile updater
Fix system Python

0.5. INSTALLING ON UBUNTU LINUX

0.6. INSTALLING ON OTHER PLATFORMS

your operating system

0.7. USING THE PYTHON SHELL

help mode

0.8. PYTHON EDITORS AND IDES

CHAPTER 1. YOUR FIRST PYTHON PROGRAM


Dont bury your burden in saintly silence. You have a problem? Great. Rejoice, dive in, and investigate.
Ven. Henepola Gunaratana

1.1. DIVING IN

modules

1.2. DECLARING FUNCTIONS

When you
need a
function,
just declare
it.

1.2.1. OPTIONAL AND NAMED ARGUMENTS

optional

1.3. WRITING READABLE CODE

i.e.

1.3.1. DOCUMENTATION STRINGS

Every
function

deserves a
decent
docstring.

1.4. THE

SEARCH PATH

built-in modules

1.5. EVERYTHING IS AN OBJECT

1.5.1. WHATS AN OBJECT?

all

must

but they could

first-class objects

first-class objects

everything in Python is an
object

1.6. INDENTING CODE

not

1.7. EXCEPTIONS

check

handle

unhandled

handled

1.7.1. CATCHING IMPORT ERRORS

if it exists

How try except can be used for


importing modules.

some
did

1.8. UNBOUND VARIABLES

not

1.9. EVERYTHING IS CASE-SENSITIVE

1.10. RUNNING SCRIPTS

Everything
in Python is
an object.

1.11. FURTHER READING

Python Reference Manual

CHAPTER 2. NATIVE DATATYPES


Wonder is the foundation of all philosophy, inquiry its progress, ignorance its end.
Michel de Montaigne

2.1. DIVING IN

Booleans
Numbers
Strings
Bytes

e.g.
byte arrays e.g.

Lists
Tuples
Sets
Dictionaries

module function class method file

compiled code

&

&

2.2. BOOLEANS

You can use


boolean contexts

virtually
any
expression
in a boolean
context.

2.3. NUMBERS

2.3.1. COERCING INTEGERS TO FLOATS AND VICE-VERSA

2.3.2. COMMON NUMERICAL OPERATIONS

2.3.3. FRACTIONS

2.3.4. TRIGONOMETRY

2.3.5. NUMBERS IN A BOOLEAN CONTEXT

Zero values
are false,
and nonzero values
are true.

2.4. LISTS

&

2.4.1. CREATING A LIST

2.4.2. SLICING A LIST

slicing

a_list[0] is
the first
item of
a_list.

2.4.3. ADDING ITEMS TO A LIST

four

is itself a list

2.4.4. SEARCHING FOR VALUES IN A LIST

where

first

not

2.4.5. REMOVING ITEMS FROM A LIST

Lists never
have gaps.

not

value

2.4.6. REMOVING ITEMS FROM A LIST: BONUS ROUND

and returns the value


it removed

2.4.7. LISTS IN A BOOLEAN CONTEXT

Empty lists
are false; all
other lists
are true.

2.5. TUPLES

can

immutable

2.5.1. TUPLES IN A BOOLEAN CONTEXT

2.5.2. ASSIGNING MULTIPLE VALUES AT ONCE

2.6. SETS

2.6.1. CREATING A SET

promise

unordered

is

2.6.2. MODIFYING A SET

unique values
still

2.6.3. REMOVING ITEMS FROM A SET

all

2.6.4. COMMON SET OPERATIONS

either
both
exactly one

looks

2.6.5. SETS IN A BOOLEAN CONTEXT

2.7. DICTIONARIES

2.7.1. CREATING A DICTIONARY

2.7.2. MODIFYING A DICTIONARY

2.7.3. MIXED-VALUE DICTIONARIES

is

2.7.4. DICTIONARIES IN A BOOLEAN CONTEXT

Empty
dictionaries
are false; all
other
dictionaries
are true.

2.8.

2.8.1.

IN A BOOLEAN CONTEXT

2.9. FURTHER READING

CHAPTER 3. COMPREHENSIONS
Our imagination is stretched to the utmost, not, as in fiction, to imagine things which are not really there, but just
to comprehend those things which are.
Richard Feynman

3.1. DIVING IN

3.2. WORKING WITH FILES AND DIRECTORIES

3.2.1. THE CURRENT WORKING DIRECTORY

There is
always a
current
working
directory.

3.2.2. WORKING WITH FILENAMES AND DIRECTORY NAMES

Do not

3.2.3. LISTING DIRECTORIES

The
module uses
shell-like
wildcards.

3.2.4. GETTING FILE METADATA

3.2.5. CONSTRUCTING ABSOLUTE PATHNAMES

i.e.

3.3. LIST COMPREHENSIONS

You can use


any Python
expression
in a list
comprehension.

3.4. DICTIONARY COMPREHENSIONS

3.4.1. OTHER FUN STUFF TO DO WITH DICTIONARY COMPREHENSIONS

3.5. SET COMPREHENSIONS

3.6. FURTHER READING

CHAPTER 4. STRINGS
Im telling you this cause youre one of my friends.
My alphabet starts where your alphabet ends!
Dr. Seuss, On Beyond Zebra!

4.1. SOME BORING STUFF YOU NEED TO UNDERSTAND BEFORE YOU


CAN DIVE IN

F
!@#$%&

character encoding

Everything
you thought
you knew
about
strings is
wrong.
&

&

&

those

4.2. UNICODE
Enter Unicode.

every

every

this

guarantee
really

UTF-8

variable-length

&

4.3. DIVING IN

i.e.

4.4. FORMATTING STRINGS

Strings can
be defined

with either
single or
double
quotes.

Strings are objects


replacement fields

4.4.1. COMPOUND FIELD NAMES

&

format specifiers can


access items and properties of data structures using (almost)
Python syntax

compound field names

{0} is
replaced by
the 1st
format()
Any combination of the above

argument.
{1} is
replaced by
the 2nd.

e.g.
e.g.

4.4.2. FORMAT SPECIFIERS

format specifier

i.e.

4.5. OTHER COMMON STRING METHODS

4.5.1. SLICING A STRING


slicing

4.6. STRINGS VS. BYTES

string

bytes

can never do

entirely different sequence of bytes

4.7. POSTSCRIPT: CHARACTER ENCODING OF PYTHON SOURCE CODE


i.e.

4.8. FURTHER READING

i.e.

CHAPTER 5. REGULAR EXPRESSIONS


Some people, when confronted with a problem, think I know, Ill use regular expressions. Now they have two
problems.
Jamie Zawinski

5.1. DIVING IN

&

more

5.2. CASE STUDY: STREET ADDRESSES

not

^ matches
the start of
a string. $
matches the
end of a
string.

really

and

raw string

*sigh*

5.3. CASE STUDY: ROMAN NUMERALS

5.3.1. CHECKING FOR THOUSANDS

5.3.2. CHECKING FOR HUNDREDS

? makes a
pattern
optional.

not

5.4. USING THE

SYNTAX

{1,4}
matches
between 1
and 4
occurrences
of a pattern.

does not match

5.4.1. CHECKING FOR TENS AND ONES

fails to match

(A|B)
matches
either
pattern A or
pattern B,
but not
both.

5.5. VERBOSE REGULAR EXPRESSIONS

verbose regular expressions

5.6. CASE STUDY: PARSING PHONE NUMBERS

does

\d matches
any numeric
digit (09).
\D matches
anything
but digits.

and then remember them as a group that I can ask for later

without

except

without

5.7. SUMMARY

remembered group

CHAPTER 6. CLOSURES & GENERATORS


My spelling is Wobbly. Its good spelling but it Wobbles, and the letters get in the wrong places.
Winnie-the-Pooh

6.1. DIVING IN

Bass

basses fax

faxes
coach

rashes
cheetahs

waltz

waltzes

coaches
cheetah

rash

vacancy

vacancies

Man
humans Mouse
wife

wives
sheep deer

mice
lowlife

louse

day

men
lice

house

days

woman

women
houses Knife

lowlifes

haiku

6.2. I KNOW, LETS USE REGULAR EXPRESSIONS!

human
knives

Regular Expressions

all

except

not

6.3. A LIST OF FUNCTIONS

The rules
variable is a
sequence of
pairs of
functions.

6.4. A LIST OF PATTERNS

closures

strings

6.5. A FILE OF PATTERNS

context

6.6. GENERATORS

that

generator

&

6.6.1. A FIBONACCI GENERATOR

yield
pauses a
function.
next()
resumes
where it left
off.

6.6.2. A PLURAL RULE GENERATOR

And then you yield.


on demand

and

that

6.7. FURTHER READING

CHAPTER 7. CLASSES & ITERATORS


East is East, and West is West, and never the twain shall meet.
Rudyard Kipling

7.1. DIVING IN

iterators

iterators

7.2. DEFINING CLASSES

can

7.2.1. THE

METHOD

not

7.3. INSTANTIATING CLASSES

7.4. INSTANCE VARIABLES

7.5. A FIBONACCI ITERATOR


Now

All three of these class


methods,

,
, and
, begin and

end with a pair of


underscore ( )
characters. Why is that?
Theres nothing magical
about it, but it usually
indicates that these are
special methods. The
only thing special
about special methods is
that they arent called
directly; Python calls
them when you use some
other syntax on the class
or an instance of the
class. More about special
methods.

7.6. A PLURAL RULE ITERATOR

iter(f) calls
f.__iter__
next(f) calls
f.__next__

any

class variable

File objects are iterators too! Its iterators all


the way down

that
i.e.

and

again

not

Minimal startup cost.

Maximum performance.

Separation of code and data.

long

7.7. FURTHER READING

CHAPTER 8. ADVANCED ITERATORS


Great fleas have little fleas upon their backs to bite em,
And little fleas have lesser fleas, and so ad infinitum.
Augustus De Morgan

8.1. DIVING IN

cryptarithms

alphametics

in just 14 lines of code

The most
well-known
alphametic
puzzle is

8.2. FINDING ALL OCCURRENCES OF A PATTERN

This is the
hardest
tongue
twister in
it doesnt return
overlapping matches

the English
language.

8.3. FINDING THE UNIQUE ITEMS IN A SEQUENCE

8.4. MAKING ASSERTIONS

8.5. GENERATOR EXPRESSIONS

e.g.

8.6. CALCULATING PERMUTATIONS THE LAZY WAY!

The
module has
all kinds of
fun stuff.

8.7. OTHER FUN STUFF IN THE

MODULE

i.e.

already sorted by length

longest

really

8.8. A NEW KIND OF STRING MANIPULATION

byte

Now youre
getting to
the really
fun part.

8.9. EVALUATING ARBITRARY STRINGS AS PYTHON EXPRESSIONS

any
any

eval() is
EVIL
NOT

I cant imagine

some

WILL

any

i.e.

will

is

8.10. PUTTING IT ALL TOGETHER


i.e.

unique

8.11. FURTHER READING

CHAPTER 9. UNIT TESTING


Certitude is not the test of certainty. We have been cocksure of many things that were not so.
Oliver Wendell Holmes, Jr.

9.1. (NOT) DIVING IN

K
paper

hand
punchcards

liked it!

test-driven development

sir

9.2. A SINGLE QUESTION

Every test is
an island.

anything
does

right

Write a test
that fails,
then code
until it
passes.

Now

9.3. HALT AND CATCH FIRE

The
Pythonic
way to halt
and catch
fire is to

baaaaaaad

raise an
exception.

three

9.4. MORE HALTING, MORE FIRE

thats

not

9.5. AND ONE MORE THING

9.6. A PLEASING SYMMETRY

and

9.7. MORE BAD INPUT

CHAPTER 10. REFACTORING


After one has played a vast quantity of notes and more notes, it is simplicity that emerges as the crowning reward
of art.
Frdric Chopin

10.1. DIVING IN

Now

seem

all

10.2. HANDLING CHANGING REQUIREMENTS

10.3. REFACTORING

prove

you dont need to use regular expressions at all

when the module is imported

10.4. SUMMARY

before

CHAPTER 11. FILES


A nine mile walk is no joke, especially in the rain.
Harry Kemelman, The Nine Mile Walk

11.1. DIVING IN

M
11.2. READING FROM TEXT FILES

relative path

11.2.1. CHARACTER ENCODING REARS ITS UGLY HEAD

The default
encoding is
platformdependent.
platform-dependent

11.2.2. STREAM OBJECTS

stream object

might

11.2.3. READING DATA FROM A TEXT FILE

Always
specify an
parameter
when you
open a file.

bytes
characters

11.2.4. CLOSING FILES

that

not

11.2.5. CLOSING FILES AUTOMATICALLY

is good.
is
better.

Python calls

automatically

11.2.6. READING DATA ONE LINE AT A TIME

Python handles line endings automatically

the
stream object is also an iterator

11.3. WRITING TO TEXT FILES

Just open a
file and

start
writing.

never

11.3.1. CHARACTER ENCODING AGAIN

strings

bytes

11.4. BINARY FILES

the
number of bytes to read

11.5. STREAM OBJECTS FROM NON-FILE SOURCES

an arbitrary stream object

To read
from a fake
file, just call
.

youre not
limiting yourself to real files

11.5.1. HANDLING COMPRESSED FILES

directly read or write a gzip-compressed file

11.6. STANDARD INPUT, OUTPUT, AND ERROR

,
,
.

11.6.1. REDIRECTING STANDARD OUTPUT

two

i.e.

i.e.

a comma-separated list of contexts

11.7. FURTHER READING

CHAPTER 12. XML


In the archonship of Aristaechmus, Draco enacted his ordinances.
Aristotle

12.1. DIVING IN

12.2. A 5-MINUTE CRASH COURSE IN XML

document
elements

start and end tags

start tag
end tag
closed
nested

subelement

child

root element
not an

XML

document

attributes
Attribute names

Attribute values

text content

empty

modules
namespaces

default namespace

prefix

identical

12.3. THE STRUCTURE OF AN ATOM FEED

&

root element

12.4. PARSING XML

not

local name

12.4.1. ELEMENTS ARE LISTS

direct

12.4.2. ATTRIBUTES ARE DICTONARIES

12.5. SEARCHING FOR NODES WITHIN AN XML DOCUMENT

direct children

i.e.

is

descendant

i.e.

any
is

12.6. GOING FURTHER WITH LXML

nodes

12.7. GENERATING XML

default namespace

12.8. PARSING BROKEN XML

not

no guarantee of interoperability

12.9. FURTHER READING

CHAPTER 13. SERIALIZING PYTHON OBJECTS


Every Saturday since weve lived in this apartment, I have awakened at 6:15, poured myself a bowl of cereal,
added
a quarter-cup of 2% milk, sat on this end of this couch, turned on BBC America, and watched Doctor Who.
Sheldon, The Big Bang Theory

13.1. DIVING IN

13.1.1. A QUICK NOTE ABOUT THE EXAMPLES IN THIS CHAPTER

13.2. SAVING DATA TO A PICKLE FILE

serializes

13.3. LOADING DATA FROM A PICKLE FILE


i.e.

perfect replica

13.4. PICKLING WITHOUT A FILE

13.5. BYTES AND STRINGS REAR THEIR UGLY HEADS AGAIN

13.6. DEBUGGING PICKLE FILES

13.7. SERIALIZING PYTHON OBJECTS TO BE READ BY OTHER


LANGUAGES

13.8. SAVING DATA TO A J S O N FILE

is

13.9. MAPPING OF PYTHON DATATYPES TO J S O N

Notes

JSON

Python 3

&

13.10. SERIALIZING DATATYPES UNSUPPORTED BY J S O N

thats

returns a Python
dictionary

13.11. LOADING DATA FROM A J S O N FILE

list

13.12. FURTHER READING

CHAPTER 14. HTTP WEB SERVICES


A ruffled mind makes a restless pillow.
Charlotte Bront

14.1. DIVING IN

14.2. FEATURES OF HTTP

14.2.1. CACHING

latency

means
don't bug
me until
next week.

A year!
without
generating any network activity whatsoever

dont

its

14.2.2. LAST-MODIFIED CHECKING

might

means
same shit,
different
day.

hasnt

it doesnt re-send the data

really

14.2.3. ETAG CHECKING

only

means
theres
nothing new
under the
sun.

the quotation marks are part of the value

14.2.4. COMPRESSION

14.2.5. REDIRECTS

means look
over there!
temporary redirect
permanent redirect

14.3. HOW NOT TO FETCH DATA OVER HTTP

e.g.

14.4. WHATS ON THE WIRE?

i.e.

Were
downloading
3070 bytes
when we
could have
just
downloaded
941.

isnt

14.5. INTRODUCING

14.5.1. A SHORT DIGRESSION TO EXPLAIN WHY

RETURNS BYTES INSTEAD OF

STRINGS

i.e.

both

14.5.2. HOW

HANDLES CACHING

Nothing appears to happen.

Whats on
the wire?
Absolutely
nothing.

automatically

by default

all

in
both directions

and as part of the outgoing request

not

next

14.5.3. HOW

HANDLES

AND

HEADERS

freshness indicators
does not generate a single
byte of network activity
might
validators
and no data

and no data
two
last time

14.5.4. HOW

HANDLES COMPRESSION

We have
both kinds
of music,
country
AND
western.

14.5.5. HOW

HANDLES REDIRECTS

final

didnt

it checked its cache before issuing another request

didnt

without hitting the


network for the original

URL

14.6. BEYOND HTTP GET

Identi.ca

REST API

Method: statuses/update

URL

Formats

HTTP

Method(s)

Requires Authentication

Parameters

a.k.a.

payload
requires
U R L -encoded

i.e.

payload

payload

14.7. BEYOND HTTP POST

please

14.8. FURTHER READING

CHAPTER 15. CASE STUDY: PORTING


PYTHON 3

TO

Words, words. Theyre all we have to go on.


Rosencrantz and Guildenstern are Dead

15.1. DIVING IN

15.2. WHAT IS CHARACTER ENCODING AUTO-DETECTION?

15.2.1. ISNT THAT IMPOSSIBLE?

15.2.2. DOES SUCH AN ALGORITHM EXIST?

15.3. INTRODUCING THE

MODULE

Encoding
detection is
really
language
detection in
drag.

15.3.1. U T F - N WITH A B O M

15.3.2. ESCAPED ENCODINGS

15.3.3. MULTI-BYTE ENCODINGS

15.3.4. SINGLE-BYTE ENCODINGS

Seriously,
wheres my
Unicode
pony?

15.3.5.

15.4. RUNNING

can

was

15.5. A SHORT DIGRESSION INTO MULTI-FILE MODULES


multi-file module
If
Python sees an
module.

file in a directory, it assumes that all of the files in that directory are part of the same

relative import
only

15.6. FIXING WHAT


15.6.1.

CANT

IS INVALID SYNTAX

You do have
tests, right?

15.6.2. NO MODULE NAMED

the logic behind relative


imports has changed in Python 3

15.6.3. NAME

IS NOT DEFINED

open() is the
new file().
PapayaWhip
is the new
black.

15.6.4. CANT USE A STRING PATTERN ON A BYTES-LIKE OBJECT

bytes

Not an
array of
characters,
but an

array of
bytes.

15.6.5. CAN'T CONVERT

OBJECT TO

IMPLICITLY

crash

two

15.6.6. UNSUPPORTED OPERAND TYPE(S) FOR +:

AND

second time

Each item in
a string is a
string. Each
item in a
byte array
is an
integer.

make sure it stays a byte


array

15.6.7.

EXPECTED STRING OF LENGTH

1, BUT

FOUND

15.6.8. UNORDERABLE TYPES:

>=

15.6.9. GLOBAL NAME

IS NOT DEFINED

/me does a little dance

15.7. SUMMARY

You
only

CHAPTER 16. PACKAGING PYTHON LIBRARIES


Youll find the shame is like the pain; you only feel it once.
Marquise de Merteuil, Dangerous Liaisons

16.1. DIVING IN

R
setup script

16.2. THINGS DISTUTILS CANT DO FOR YOU

Choose a license

Classify your software


Write a read me file

16.3. DIRECTORY STRUCTURE

you

16.4. WRITING YOUR SETUP SCRIPT

name
version
author
author_email
url

description
long_description
classifiers

16.5. CLASSIFYING YOUR PACKAGE

not

Dont do that.
always
Programming Language

License

the absolute first thing I look for

Operating System

Development Status
Intended Audience
Framework
Topic

16.5.1. EXAMPLES OF GOOD PACKAGE CLASSIFIERS

16.6. SPECIFYING ADDITIONAL FILES WITH A MANIFEST

manifest file

not

16.7. CHECKING YOUR SETUP SCRIPT FOR ERRORS

16.8. CREATING A SOURCE DISTRIBUTION

16.9. CREATING A GRAPHICAL INSTALLER

16.9.1. BUILDING INSTALLABLE PACKAGES FOR OTHER OPERATING SYSTEMS

16.10. ADDING YOUR SOFTWARE TO THE PYTHON PACKAGE INDEX

NAME

16.11. THE MANY POSSIBLE FUTURES OF PYTHON PACKAGING

16.12. FURTHER READING

CHAPTER 17. PORTING CODE TO PYTHON 3 WITH

Life is pleasant. Death is peaceful. Its the transition thats troublesome.


Isaac Asimov (attributed)

17.1. DIVING IN

S
can

17.2.

Notes

STATEMENT

Python 2

Python 3

17.3. UNICODE STRING LITERALS

Notes

17.4.

Python 2

Python 3

GLOBAL FUNCTION

Notes

17.5.

Notes

Python 2

Python 3

DATA TYPE

Python 2

Python 3

17.6. <> COMPARISON

Notes

Python 2

Python 3

17.7.

Notes

DICTIONARY METHOD

Python 2

Python 3

do

17.8. DICTIONARY METHODS THAT RETURN LISTS

Notes

Python 2

Python 3

no change

17.9. MODULES THAT HAVE BEEN RENAMED OR REORGANIZED

17.9.1.

Notes

Python 2

Python 3

17.9.2.

Notes

Python 2

Python 3

Notes

Python 2

Python 3

17.9.3.

Notes

Python 2

Python 3

17.9.4.

Notes

Python 2

Python 3

17.9.5. OTHER MODULES


Notes

Python 2

Python 3

17.10. RELATIVE IMPORTS WITHIN A PACKAGE

Notes

Python 2

17.11.

Notes

Python 3

ITERATOR METHOD

Python 2

Python 3

no change

17.12.

Notes

GLOBAL FUNCTION

Python 2

Python 3
no change

no change
no change

already

17.13.

Notes

GLOBAL FUNCTION

Python 2

Python 3

no change
no change

17.14.

Notes

GLOBAL FUNCTION

Python 2

17.15.

Notes

Python 3

GLOBAL FUNCTION

Python 2

Python 3

17.16.

Notes

17.17.

Notes

17.18.

GLOBAL FUNCTION

Python 2

Python 3

STATEMENT

Python 2

Python 3

STATEMENT

Notes

17.19.

Notes

Python 2

Python 3

LITERALS (BACKTICKS)

Python 2

Python 3

&

17.20.

Notes

STATEMENT

Python 2

Python 3

no change

no change

all

all

17.21.

Notes

STATEMENT

Python 2

Python 3
unchanged

unsupported

17.22.

Notes

METHOD ON GENERATORS

Python 2

Python 3
no change

unsupported

with
only

17.23.

Notes

GLOBAL FUNCTION

Python 2

Python 3

no change
no change

not

17.24.

Notes

AND

Python 2

GLOBAL FUNCTIONS

Python 3

17.25.

Notes

FUNCTION ATTRIBUTES

Python 2

Python 3

docstring

17.26.

Notes

I/O METHOD

Python 2

Python 3
no change (broken)

17.27.

FUNCTIONS THAT TAKE A TUPLE INSTEAD OF MULTIPLE

PARAMETERS

Notes

Python 2

Python 3

unchanged

17.28. SPECIAL METHOD ATTRIBUTES

Notes

17.29.

Python 2

Python 3

SPECIAL METHOD

Notes

Python 2

Python 3

no change

17.30. OCTAL LITERALS

Notes

Python 2

Python 3

Python 2

Python 3

17.31.

Notes

17.32.

Notes

17.33.

Notes

GLOBAL FUNCTION

Python 2

Python 3

GLOBAL FUNCTION

Python 2

Python 3
no change

17.34.

Notes

EXCEPTION

Python 2

Python 3

17.35.

Notes

MODULE CONSTANTS

Python 2

Python 3

17.36.

Notes

GLOBAL FUNCTION

Python 2

17.37.

Notes

DATATYPE

Python 2

17.38.

Notes

Python 3

Python 3

MODULE

Python 2

Python 3

17.39.

Notes

Python 2

Python 3

17.40. LIST COMPREHENSIONS OVER TUPLES

Notes

Python 2

Python 3

17.41.

Notes

FUNCTION

Python 2

Python 3

17.42. METACLASSES

Notes

Python 2

Python 3
unchanged

17.43. MATTERS OF STYLE

17.43.1.

Notes

17.43.2.

LITERALS (EXPLICIT)

Before

After

GLOBAL FUNCTION (EXPLICIT)

Notes

Before

After

17.43.3. WHITESPACE AROUND COMMAS (EXPLICIT)

Notes

Before

After

17.43.4. COMMON IDIOMS (EXPLICIT)

Notes

Before

After

CHAPTER 18. SPECIAL METHOD NAMES


My specialty is being right when other people are wrong.
George Bernard Shaw

18.1. DIVING IN

T
18.2. BASICS

Notes

You Want

So You Write

after

And Python Calls

New in Python 3

18.3. CLASSES THAT ACT LIKE ITERATORS

Notes

You Want

So You Write

And Python
Calls

18.4. COMPUTED ATTRIBUTES


Notes

You Want

So You Write

And Python Calls

every reference to any attribute or


method name
not

all

is still called
is called unconditionally

because all attribute and method lookups go through the


method

18.5. CLASSES THAT ACT LIKE FUNCTIONS

Notes

You Want

So You Write

And Python Calls

18.6. CLASSES THAT ACT LIKE SETS

Notes

You Want

So You Write

And Python
Calls

18.7. CLASSES THAT ACT LIKE DICTIONARIES

Notes

You Want

So You Write

And Python Calls

18.8. CLASSES THAT ACT LIKE NUMBERS

Notes

You Want

So You Write

And Python
Calls

&

not

reflected operands
e.g.

Notes

You Want

So You Write

And Python
Calls

&

Notes

You Want

So You Write

And Python
Calls

Notes

You Want

So You Write

18.9. CLASSES THAT CAN BE COMPARED

And Python Calls

Notes

You Want

So You Write

And Python
Calls

18.10. CLASSES THAT CAN BE SERIALIZED

Notes

You Want

So You Write

And Python Calls

18.11. CLASSES THAT CAN BE USED IN A

Notes

You Want

So You Write

BLOCK

And Python Calls

18.12. REALLY ESOTERIC STUFF

Notes

You Want

So You Write

18.13. FURTHER READING

And Python Calls

CHAPTER 19. WHERE TO GO FROM HERE


Go forth on your path, as it exists only through your walking.
St. Augustine of Hippo (attributed)

19.1. THINGS TO READ

&

19.2. WHERE TO LOOK FOR PYTHON 3-COMPATIBLE CODE

CHAPTER 20. TROUBLESHOOTING


Wheres the

key?

variously attributed

20.1. DIVING IN

F
20.2. GETTING TO THE COMMAND LINE

20.3. RUNNING PYTHON ON THE COMMAND LINE

Você também pode gostar