Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Is there a difference between python2.7 and 3.0?
Is there a difference between python2.7 and 3.0?
There is a difference.

1. performance

Py3.0 runs pystone benchmark 30% slower than Py2.5. Guido thinks that Py3.0 has a lot of room for optimization, and it can be used for string and shaping operations.

So as to obtain good optimization results. ?

The performance of Py3. 1 is slower than that of Py2.5 15%, and there is still much room for improvement. ?

2. coding?

By default, Py3. The X source file uses utf-8 encoding, which makes the following code legal:?

& gt& gt& gt China =' China'

& gt& gt& gt print?

China?

3.grammar?

1) has been deleted

2) Delete`` and replace all with repr ()?

3) keywords include as and with, as well as True, False None none?

4) integer division returns floating-point numbers. For integer results, use//?

5) Add non-local statements. Can you directly assign peripheral (non-global) variables with noclocal x?

6) Delete the print statement and add the print () function to achieve the same function. The same is true of the exec statement, which has been changed to the exec () function?

For example:?

2.x: Print "Yes", 2*2?

3.x: print ("the answer is", 2*2)?

2.x: print x, # Don't wrap at the end of comma?

3.X: print(x, end=" ") # Use spaces instead of line breaks?

2.X: print # output a new line?

3.X: print() # Output a new line?

2.x:print & gt; & gtsys.stderr, "fatal error"?

3.X: print ("fatal error", file=sys.stderr)?

2.X: print (x,y)? # output repr((x,y))?

3.X: print((x, y)) # is different from print(x, y)! ?

7) Changed the behavior of sequential operators, such as X.

8) The input function has changed. Raw_input has been deleted, and input has been used instead:?

2.x: guess = int (raw _ input ('Enter an integer:') # How to read keyboard input?

3.X:guess = int(input ('enter an integer:'))

9) Remove tuple parameters and unpack. Can't def(a, (b, c)):pass define a function like this?

10) is a new octal word variable, and the oct () function has been modified accordingly. ?

2. the way of x is as follows:?

& gt& gt& gt0666?

438?

& gt& gt& gt October (438)?

'0666'?

3.x like this:?

& gt& gt& gt0666?

Syntax error: invalid tag (& ltpyshell # 63>, line 1)?

& gt& gt& gt0o666?

438?

& gt& gt& gt October (438)?

0o666 '?

1 1) plus binary text and bin () function?

& gt& gt& gt bin (438)?

0b 1 10 1 10 1 10 '?

& gt& gt& gt_ 438 = ' 0b 1 10 1 10 1 10 '?

& gt& gt& gt_438?

0b 1 10 1 10 1 10 '?

12) extended iterative unpacking. In Py3. X, a, b, *rest = seq and *rest, a = seq are all legal, only two points are needed: Is rest a list?

Objects and sequences are iterative. ?

13) The new super () can no longer pass parameters to super ().

& gt& gt& gtC class (object):?

def __init__(self,a):?

Print ('c', a)?

& gt& gt& gtD class (class c):?

def __init(self,a):?

Super (). __init__(a) # Call super () without parameters?

& gt& gt& gtD(8)?

C 8?

& lt__main__。 D object is located at 0x00d7ed90 > ?

New metaclass grammar:

Foo class (* bases, **kwds):?

Pass?

15) supports class decorators. Usage is the same as function decorator:?

& gt& gt& gtdef foo(cls_a):?

def print_func(self):?

Print ("Hello, world!") )?

cls_a.print = print_func?

Return to cls_a?

& gt& gt& gt@foo?

Class c (object):?

Pass?

& gt& gt& gtc()。 print()?

hello,world ?

Class decorator can be used to play the big trick of changing civet cats into princes. See PEP 3 129 for details?

4. String and byte string?

1) now has only one type of string, but it is similar to unicode version 2. x ..

2) For byte strings, please refer to Item 2 of data type?

5. Data type?

1)Py3。 X has removed the long type, and now there is only one integer-int, but it behaves like 2. X dragon?

2) Added byte type, corresponding to version 2 octet string. X. The method of defining a byte literal is as follows:?

& gt& gt& gt China?

Type & gt> (b)?

& lt type "byte">?

String objects and byte objects can be used. encode()(str-> >; Bytes) or. Decode () (byte-> Str) methods are converted to each other. ?

& gt& gt& gts = b.decode()?

& gt& gt& gts?

China?

& gt& gt& gtb 1 = s.encode()?

& gt& gt& gtb 1?

China?

3) dictionary. keys(),。 Projects and. The values () method returns an iterator, and the previous functions such as iterkeys () are abandoned. Also withdrew?

Dict.has_key (), replace with in?

6. Object-oriented?

1) introduces the Abstraact base class (ABCs). ?

2) Both the container class and the iterator class are ABCs, so there are many more types in the cellections module than in Py2.5?

& gt& gt& gt Import collection?

& gt& gt& gt print ('\n'). join(dir(collections)))?

Redeemable?

Container?

Huh? Chable?

ItemsView?

Iterative?

Iterator?

KeysView?

Mapping?

MappingView?

Variable mapping?

Variable sequence?

MutableSet?

NamedTuple?

Sequence?

Settings?

The right size?

ValuesView?

__all__?

_ _ built _ _?

__doc__?

__file__?

__name__?

_abcoll?

_itemgetter?

_sys?

defaultdict?

Deque?

In addition, numerical types are ABC-based. Please refer to PEP 3 1 19 and PEP 3 14 1. ?

3) Rename the next () method of the iterator to _ _ next _ (), and add a built-in function next () to call the __next__ () method of the iterator?

4) Two decorator, @abstractmethod and @abstractproperty are added, which makes it more convenient to write abstract methods (properties). ?

7. abnormal?

1) so all exceptions are inherited from BaseException and StardardError is deleted?

2) Delete the sequence behavior and. Message properties of the exception class.

3) use raise Exception(args) instead of raise Exception, args syntax?

4) Capture the grammatical changes of exceptions and introduce as keywords to identify abnormal instances. In Py2.5:?

& gt& gt& gt try:

...? Raise notimplementererror(' Error')?

... except NotImplementedError, error:

...? Print error. News?

...?

Mistakes?

In Py3.0:?

& gt& gt& gt try:

Raise notimplementererror(' Error')?

Except not implemented error as error: # Pay attention to this as?

print(str(error))?

Mistakes?

5) Exception chain, because __context__ is not implemented in 3.0a 1 version?

8. Module changes?

1) deleted the cPickle module, and can be replaced by the Pickle module. Eventually we will have a transparent and efficient module. ?

2) removed the imageop module?

3) Audio Dev, Bastion, BSD DB 185, Exception, Linux Audio Dev, MD5, MimeWriter, Mimi FY, Popen2,

Rexec, sets, sha, Stringold, Strop, Sun Audio Dev, Timing and xmllib modules?

The bsddb module (released separately and available from http://www.jcea.es/programacion/pybsddb.htm) has been removed?

5) Remove the new module?

6) 6) The functions of os.tmpnam () and os.tmpfile () are moved to the tmpfile module?

7)tokenize module can now process bytes. The main entry point is no longer generate_tokens, but tokenize.tokenize ()?

9. Others?

1)xrange () was renamed as range (). To get a list using range (), you must explicitly call:?

& gt& gt& gtlist (range (10))?

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]?

2)bytes objects cannot be hash, and the methods of b.lower (), b.strip () and b.split () are not supported, and the latter two can be used.

\n\t\r \f') and b.split (b'') to achieve the same goal?

3)zip (), map () and filter () all return iterators. And apply (), callable (), constent (), execfile (), reduce () and reload?

The () function has been deleted.

Now hasattr () can be used instead of the syntax of callable (). Hasattr (), such as hasattr (string,' _ _ name _').

4) Character strings, letters and their correlation. Lowercase sum. Capital letters have been deleted. Please use string.ascii_letters instead?

5) if x

6) The _ _ getslice _ _ series members were abandoned. A[i:j] is converted into a. _ _ getitem _ (slice (I, j)) or __setitem__ and?

_ _ delivery _ _ call?

7)7)file class is abandoned, in Py2.5:?

& gt& gt& gt file?

& lt type "file">?

In Py3. x:?

& gt& gt& gt file?

Backtracking (last call):?

File "& ltpyshell #120 >;" , line 1, in < lt module >?

Documents?

Name error: the name "file" is not defined.