Differences between python3.0 and 2.x

Source: Internet
Author: User
Tags integer division

Reposted from Network

1. Performance
Py3.0 runs pystone benchmark 30% slower than Py2.5. Guido believes that Py3.0 has a huge space for optimization and can achieve good optimization results in string and integer operations.

2. Encoding
The Py3.0 source code file uses UTF-8 encoding by default, which makes the following code Legal:

>>> China >>>

3. Syntax
1) Remove <>, switch to all! =
2) Remove ''and use repr () instead ()
3) Add the as and with keywords, and add True, False, None
4) The integer division function returns a floating point number. To obtain the integer result, use //
5) Add the nonlocal statement. You can use noclocal x to directly assign peripheral (non-Global) variables.
6) Remove the print statement and add the print () function to implement the same function. The exec statement has been changed to exec ()

4. Functions

1) The print statement is replaced by the print () function. You can use the keyword parameter to replace the special syntax of the old print statement. For example:

Old:  , 2*2(, 2*2 x, New: (x, end= )  New: ()  >>sys.stderr, (, file= (x, y) New: ((x, y)) 

2) changes the behavior of sequence operators. For example, if x <y does not match the y type, a TypeError is thrown instead of returning the immediate bool value.

3) The input function has changed:

= int(raw_input()) = int(input())

4) Remove the productkey parameter from the package. Do not define functions like def (a, (B, c): pass

5) The new octal variable modifies the oct () function accordingly. 2. The method of x is as follows:

>>> 0666438>>> oct(4383>>> 0666<pyshell>>>438>>> oct(438

6) added the binary literal and bin () functions.

>>> bin(438>>> _438 = >>>

7) scalable and iterative unpacking. In Py3.0, a, B, * rest = seq and * rest, a = seq are both valid and only two points are required: rest is a list object and seq can be iterated.

8) the new super () can no longer pass parameters to super ().

>>>  (>>>  (a) >>> D(88<.D object at 0x00D7ED90>

9) New metaclass syntax

 Foo(*bases, **

10) supports class decorator. The usage is the same as that of the decorator function:

>>>>>>>>>> C (). The decorator Can be used to play the tricks of a CAPTCHA. For more information, see PEP 3129.

5. String and byte string


1) currently, only one type of string is available, but it is almost the same as unicode in 2.x.
2) For a byte string, see the 2nd entries in "data type ".

6. Data Types


1) Py3.0 removes the long type, and now there is only one integer -- int, But it acts like the long type
2) The bytes type is added, which corresponds to the 8-Bit String Of The 2.x version. The following describes how to define a bytes literal:

>>> b = b>>><type >

3) str objects and bytes objects can be converted to each other using the. encode () (str-> bytes) or. decode () (bytes-> str) method.

>>> s =>>>>>> b1 =>>>

4) 1. dict's. keys (),. items, and. values () Methods return to the iterator, while previous iterkeys () and other functions are discarded. Dict. has_key () is also removed. Replace it with in.

7,7. Object-oriented

1) 1) Introduce abstract Base Classes (Abstraact Base Classes, ABCs ).
2) The container class and iterator class are ABCs, so the types in the cellections module are much higher than those in Py2.5.

>>> >>> (

In addition, the value type is also ABCs. For more information, see PEP 3119 and PEP 3141.

3) The next () method of the iterator is changed to _ next _ (), and the built-in function next () is added to call the _ next _ () method of the iterator.

4) added two decorator: @ abstractmethod and @ abstractproperty, which makes it easier to compile Abstract METHODS (attributes ).

8. Exception

1) the exceptions are inherited from BaseException and the StardardError is deleted.
2) Remove the sequence behavior and. message attributes of the exception class
3) use raise Exception (args) instead of raise Exception. args syntax
4) Capture abnormal syntax changes and introduce the as keyword to identify abnormal instances. In Py2.5:

>>>  NotImplementedError(>>>  NotImplementedError( NotImplementedError as error: 

5) exception chain, because _ context _ has not been implemented in version 3.0a1.

9. module changes

• Removed the cPickle module, which can be replaced by the pickle module. In the end, we will have a transparent and efficient module.
• Removed the imageop module.
• Removed audiodev, Bastion, bsddb185, exceptions, linuxaudiodev, md5, MimeWriter, mimify, popen2, rexec, sets, sha, stringold, strop, sunaudiodev, timing, and xmllib modules
• Removed the bsddb module (released separately, available at http://www.jcea.es/programacion/pybsddb.htm)
• Removed the new module.
• OS. tmpnam () and OS. tmpfile () functions are moved to the tmpfile module.
• The tokenize module currently uses bytes to work. The main entry point is not generate_tokens, but tokenize. tokenize ()

10. Others

1) xrange () is renamed to range (). To use range () to obtain a list, you must explicitly call:

>>> list(range(101, 2, 3, 4, 5, 6, 7, 8, 9]

2) bytes objects cannot be hashed or B. lower (), B. strip () and B. the split () method, but the latter two can use B. strip (B '\ n \ t \ r \ F') and B. split (B '') to achieve the same purpose
3) Both zip (), map (), and filter () return the iterator. The apply (), callable (), coerce (), execfile (), reduce (), and reload () functions are all removed
4) string. letters and related. lowercase and. uppercase are removed. Use string. ascii_letters and so on.
5) If x <y cannot be compared, A TypeError exception is thrown. 2. Version x returns a pseudo-random Boolean value.
6) _ getslice _ series members are discarded. A [I: j] is converted to a. _ getitem _ (slice (I, j) or _ setitem _ and _ delitem _ according to the context.
7) the file class is discarded. In Py2.5:

>>><type >>>>, line 1,  <module>   defined

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.