1.æ§è½
Py3.0è¿è¡ pystone benchmarkçé度æ¯Py2.5æ
¢30%ãGuido认为Py3.0ææ大çä¼å空é´ï¼å¨å符串åæ´å½¢æä½ä¸å¯
以åå¾å¾å¥½çä¼åç»æã
Py3.1æ§è½æ¯Py2.5æ
¢15%ï¼è¿æå¾å¤§çæå空é´ã
2.ç¼ç
Py3.Xæºç æ件é»è®¤ä½¿ç¨utf-8ç¼ç ï¼è¿å°±ä½¿å¾ä»¥ä¸ä»£ç æ¯åæ³çï¼
>>> ä¸å½ = 'china'
>>>print(ä¸å½)
china
3. è¯æ³
1ï¼å»é¤äº<>ï¼å
¨é¨æ¹ç¨!=
2ï¼å»é¤``ï¼å
¨é¨æ¹ç¨repr()
3ï¼å
³é®è¯å å
¥as åwithï¼è¿æTrue,False,None
4ï¼æ´åé¤æ³è¿åæµ®ç¹æ°ï¼è¦å¾å°æ´åç»æï¼è¯·ä½¿ç¨//
5ï¼å å
¥nonlocalè¯å¥ã使ç¨noclocal xå¯ä»¥ç´æ¥ææ´¾å¤å´ï¼éå
¨å±ï¼åé
6ï¼å»é¤printè¯å¥ï¼å å
¥print()å½æ°å®ç°ç¸åçåè½ãåæ ·çè¿æ execè¯å¥ï¼å·²ç»æ¹ä¸ºexec()å½æ°
ä¾å¦ï¼
2.X: print "The answer is", 2*2
3.X: print("The answer is", 2*2)
2.X: print x, # 使ç¨éå·ç»å°¾ç¦æ¢æ¢è¡
3.X: print(x, end=" ") # 使ç¨ç©ºæ ¼ä»£æ¿æ¢è¡
2.X: print # è¾åºæ°è¡
3.X: print() # è¾åºæ°è¡
2.X: print >>sys.stderr, "fatal error"
3.X: print("fatal error", file=sys.stderr)
2.X: print (x, y) # è¾åºrepr((x, y))
3.X: print((x, y)) # ä¸åäºprint(x, y)!
7ï¼æ¹åäºé¡ºåºæä½ç¬¦çè¡ä¸ºï¼ä¾å¦x<yï¼å½xåyç±»åä¸å¹é
æ¶æåºTypeErrorèä¸æ¯è¿åéå³ç boolå¼
8ï¼è¾å
¥å½æ°æ¹åäºï¼å é¤äºraw_inputï¼ç¨input代æ¿ï¼
2.X:guess = int(raw_input('Enter an integer : ')) # 读åé®çè¾å
¥çæ¹æ³
3.X:guess = int(input('Enter an integer : '))
9ï¼å»é¤å
ç»åæ°è§£å
ãä¸è½def(a, (b, c)):passè¿æ ·å®ä¹å½æ°äº
10ï¼æ°å¼ç8è¿å¶ååéï¼ç¸åºå°ä¿®æ¹äºoct()å½æ°ã
2.Xçæ¹å¼å¦ä¸ï¼
>>> 0666
438
>>> oct(438)
'0666'
3.Xè¿æ ·ï¼
>>> 0666
SyntaxError: invalid token (<pyshell#63>, line 1)
>>> 0o666
438
>>> oct(438)
'0o666'
11ï¼å¢å äº 2è¿å¶åé¢éåbin()å½æ°
>>> bin(438)
'0b110110110'
>>> _438 = '0b110110110'
>>> _438
'0b110110110'
12ï¼æ©å±çå¯è¿ä»£è§£å
ãå¨Py3.X éï¼a, b, *rest = seqå *rest, a = seqé½æ¯åæ³çï¼åªè¦æ±ä¸¤ç¹ï¼restæ¯list
对象åseqæ¯å¯è¿ä»£çã
13ï¼æ°çsuper()ï¼å¯ä»¥ä¸åç»super()ä¼ åæ°ï¼
>>> class C(object):
def __init__(self, a):
print('C', a)
>>> class D(C):
def __init(self, a):
super().__init__(a) # æ åæ°è°ç¨super()
>>> D(8)
C 8
<__main__.D object at 0x00D7ED90>
14ï¼æ°çmetaclassè¯æ³ï¼
class Foo(*bases, **kwds):
pass
15ï¼æ¯æclass decoratorãç¨æ³ä¸å½æ°decoratorä¸æ ·ï¼
>>> def foo(cls_a):
def print_func(self):
print('Hello, world!')
cls_a.print = print_func
return cls_a
>>> @foo
class C(object):
pass
>>> C().print()
Hello, world!
class decoratorå¯ä»¥ç¨æ¥ç©ç©ç¸ç«æ¢å¤ªåç大ææãæ´å¤è¯·åé
PEP 3129
4. å符串ååè串
1ï¼ç°å¨å符串åªæsträ¸ç§ç±»åï¼ä½å®è·2.xçæ¬çunicodeå ä¹ä¸æ ·ã
2ï¼å
³äºåè串ï¼è¯·åé
âæ°æ®ç±»åâç第2æ¡ç®
5.æ°æ®ç±»å
1ï¼Py3.Xå»é¤äºlongç±»åï¼ç°å¨åªæä¸ç§æ´åââintï¼ä½å®çè¡ä¸ºå°±å2.Xçæ¬çlong
2ï¼æ°å¢äºbytesç±»åï¼å¯¹åºäº2.Xçæ¬çå
«ä½ä¸²ï¼å®ä¹ä¸ä¸ªbytesåé¢éçæ¹æ³å¦ä¸ï¼
>>> b = b'china'
>>> type(b)
<type 'bytes'>
str对象åbytes对象å¯ä»¥ä½¿ç¨.encode() (str -> bytes) or .decode() (bytes -> str)æ¹æ³ç¸äºè½¬åã
>>> s = b.decode()
>>> s
'china'
>>> b1 = s.encode()
>>> b1
b'china'
3ï¼dictç.keys()ã.items å.values()æ¹æ³è¿åè¿ä»£å¨ï¼èä¹åçiterkeys()çå½æ°é½è¢«åºå¼ãåæ¶å»æçè¿æ
dict.has_key()ï¼ç¨ inæ¿ä»£å®å§
6.é¢å对象
1ï¼å¼å
¥æ½è±¡åºç±»ï¼Abstraact Base Classesï¼ABCsï¼ã
2ï¼å®¹å¨ç±»åè¿ä»£å¨ç±»è¢«ABCsåï¼æ以cellections模åéçç±»åæ¯Py2.5å¤äºå¾å¤ã
>>> import collections
>>> print('\n'.join(dir(collections)))
Callable
Container
Hashable
ItemsView
Iterable
Iterator
KeysView
Mapping
MappingView
MutableMapping
MutableSequence
MutableSet
NamedTuple
Sequence
Set
Sized
ValuesView
__all__
__builtins__
__doc__
__file__
__name__
_abcoll
_itemgetter
_sys
defaultdict
deque
å¦å¤ï¼æ°å¼ç±»åä¹è¢«ABCsåãå
³äºè¿ä¸¤ç¹ï¼è¯·åé
PEP 3119åPEP 3141ã
3ï¼è¿ä»£å¨çnext()æ¹æ³æ¹å为__next__()ï¼å¹¶å¢å å
ç½®å½æ°next()ï¼ç¨ä»¥è°ç¨è¿ä»£å¨ç__next__()æ¹æ³
4ï¼å¢å äº@abstractmethodå @abstractproperty两个 decoratorï¼ç¼åæ½è±¡æ¹æ³ï¼å±æ§ï¼æ´å æ¹ä¾¿ã
7.å¼å¸¸
1ï¼æ以å¼å¸¸é½ä» BaseException继æ¿ï¼å¹¶å é¤äºStardardError
2ï¼å»é¤äºå¼å¸¸ç±»çåºåè¡ä¸ºå.messageå±æ§
3ï¼ç¨ raise Exception(args)ä»£æ¿ raise Exception, argsè¯æ³
4ï¼æè·å¼å¸¸çè¯æ³æ¹åï¼å¼å
¥äºaså
³é®åæ¥æ è¯å¼å¸¸å®ä¾ï¼å¨Py2.5ä¸ï¼
>>> try:
... raise NotImplementedError('Error')
... except NotImplementedError, error:
... print error.message
...
Error
å¨Py3.0ä¸ï¼
>>> try:
raise NotImplementedError('Error')
except NotImplementedError as error: #注æè¿ä¸ª as
print(str(error))
Error
5ï¼å¼å¸¸é¾ï¼å 为__context__å¨3.0a1çæ¬ä¸æ²¡æå®ç°
8.模ååå¨
1ï¼ç§»é¤äºcPickle模åï¼å¯ä»¥ä½¿ç¨pickle模å代æ¿ãæç»æ们å°ä¼æä¸ä¸ªéæé«æç模åã
2ï¼ç§»é¤äºimageop模å
3ï¼ç§»é¤äº audiodev, Bastion, bsddb185, exceptions, linuxaudiodev, md5, MimeWriter, mimify, popen2,
rexec, sets, sha, stringold, strop, sunaudiodev, timingåxmllib模å
4ï¼ç§»é¤äºbsddb模å(åç¬åå¸ï¼å¯ä»¥ä»
http://www.jcea.es/programacion/pybsddb.htmè·å)
5ï¼ç§»é¤äºnew模å
6ï¼os.tmpnam()åos.tmpfile()å½æ°è¢«ç§»å¨å°tmpfile模åä¸
7ï¼tokenize模åç°å¨ä½¿ç¨byteså·¥ä½ã主è¦çå
¥å£ç¹ä¸åæ¯generate_tokensï¼èæ¯ tokenize.tokenize()
9.å
¶å®
1ï¼xrange() æ¹å为range()ï¼è¦æ³ä½¿ç¨range()è·å¾ä¸ä¸ªlistï¼å¿
é¡»æ¾å¼è°ç¨ï¼
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
2ï¼bytes对象ä¸è½hashï¼ä¹ä¸æ¯æ b.lower()ãb.strip()åb.split()æ¹æ³ï¼ä½å¯¹äºå两è
å¯ä»¥ä½¿ç¨ b.strip(bâ
\n\t\r \fâ)åb.split(bâ â)æ¥è¾¾å°ç¸åç®ç
3ï¼zip()ãmap()åfilter()é½è¿åè¿ä»£å¨ãèapply()ã callable()ãcoerce()ã execfile()ãreduce()åreload
()å½æ°é½è¢«å»é¤äº
ç°å¨å¯ä»¥ä½¿ç¨hasattr()æ¥æ¿æ¢ callable(). hasattr()çè¯æ³å¦ï¼hasattr(string, '__name__')
4ï¼string.lettersåç¸å
³ç.lowercaseå.uppercase被å»é¤ï¼è¯·æ¹ç¨string.ascii_letters ç
5ï¼å¦æx < yçä¸è½æ¯è¾ï¼æåºTypeErrorå¼å¸¸ã2.xçæ¬æ¯è¿å伪éæºå¸å°å¼ç
6ï¼__getslice__ç³»åæå被åºå¼ãa[i:j]æ ¹æ®ä¸ä¸æ转æ¢ä¸ºa.__getitem__(slice(I, j))æ __setitem__å
__delitem__è°ç¨
7ï¼file类被åºå¼ï¼å¨Py2.5ä¸ï¼
>>> file
<type 'file'>
å¨Py3.Xä¸ï¼
>>> file
Traceback (most recent call last):
File "<pyshell#120>", line 1, in <module>
file
NameError: name 'file' is not defined