A preliminary discussion of Python 3, part 1th: New features of Python 3

Source: Internet
Author: User
Tags define abstract modifiers

Python 3 is the latest version of Guido van Rossum's powerful universal programming language. Although it breaks backwards compatibility with the 2.x version, it cleans up some grammatical issues. This article is the first in a series of articles that describe various changes that affect the language and backward compatibility, and also provide several examples of new features.


Cesar Otero, consultant, freelance

February 02, 2009

    • Content

Python version 3, also known as python or py3k(nickname named after microsoft®windows®2000 OS), is the latest version of Guido van Rossum Universal programming language This. Although the new version has made a lot of improvements to the core language, it has broken backwards compatibility with the 2.x version. Other changes are something that people have long awaited, such as:

    • Real division-for example, the return of a . 5.
    • longAnd the int type is unified as a type, the suffix Lis removed.
    • True, False and None now are all keywords.

The first article in the article-python 3 Series covers new print() functions, input() changes in input/output (I/O), new bytes data types, string and string formatting changes, and built-in dict type changes. This article is intended for programmers who are familiar with Python and are interested in the changes in the new version but don't want to bother reading all of the Python Enhancement Proposal (PEP). (links to these PEP are provided in the Resources section later in this article.) )

New Print () function

Today, you'll need to get your fingers used to typing print("hello") , not the original print "hello" , because print now is a function, no longer a statement. I know, it's a little bit painful. Every Python programmer I know-once the version 3 is installed and gets the "grammatically incorrect" error-will scream in a depressed manner. I know these two extra symbols are very annoying, and I know that this will break backwards compatibility. But there is a good thing about this change.

Let's consider the case where standard output (stdout) needs to be redirected to a log. The following example opens the file Log.txt for appending and assigning the object to fid . After that, use print>> a string to redirect to the fid file:

>>>fid = open ("Log.txt", "a") >>>print>>fid, "Log Text"

Another example is redirection to standard error (SYS.STDERR):

>>>print>>sys.stderr, "An error occurred"

The above two examples are good, but there are better solutions. The new syntax only requires print() a value to be passed to the function's keyword argument file . Like what:

>>>fid = open ("Log.txt", "a") >>>print ("Log.txt", File=fid)

This code, the syntax is clearer. Another benefit is that sep you can change the delimiter (separator) by passing a string to the keyword argument, and end you can change the ending string by passing another string to the keyword argument. To change the separator, you can take advantage of:

>>>print ("Foo", "Bar", sep= "%") >>>foo%bar

In general, the new syntax is:

print ([Object, ...] [, Sep= '] [, end= ' Endline_character_here '] [, File=redirect_to_here])

Where the [] code inside the square brackets () is optional. By default, print() a newline character () is appended to the result if only itself is called \n .

Back to top of page

From Raw_input () to input ()

In Python version 2.x, raw_input() one input is read from the standard input (Sys.stdin) and a string is returned, and the trailing newline is removed from the end. The following example uses raw_input() a string from the command prompt and assigns the value to quest .

>>>quest = Raw_input ("What's Your Quest?") What is your quest? To seek the Holy Grail.>>>quest ' to seek the holy Grail. '

In contrast, the functions in Python 2.x input() require a valid Python expression, such as 3+5.

Initially, it was suggested that input() raw_input() it be removed from the Python built-in namespace, so you need to import to get the input capability. This is not true from the method, because, simply type:

>>>quest = input ("What is Your Quest?")

Will change to:

>>>import Sys>>>print ("What's Your Quest?") >>>quest = Sys.stdin.readline ()

It's too cumbersome for a simple input, and it's too hard to understand for a novice. It is often necessary to tell them how the module and the import are going, how the string output and the period operators work (so troublesome, not unlike the Java™ language). So, within Python 3, it will be renamed so that raw_input() input() data can be obtained from standard input without importing it. If you need to keep the features of version 2.x input() , you can use eval(input()) , the effect is basically the same.

Back to top of page

Introduction to bytes

The new data type bytes literal and bytes The purpose of the object is to store binary data. This object is a non-modifiable integer sequence of 0 to 127 or a purely ASCII character. In fact, it is a bytearray non-modifiable version of the object in version 2.5. A bytes literal is a string prefixed with b -for example,B ' byte literal '. The calculation of the bytes literal generates a new bytes object. You can bytes() create a new object with a function bytes . bytesthe constructor for the object is:

Bytes ([initializer[, encoding]])

For example:

>>>b = (b ' \xc3\x9f\x65\x74\x61 ') >>>print (b) B ' \xc3\x83\xc2\x9feta '

An bytes object is created, but this is superfluous because the object can be created entirely by assigning a byte literal bytes . (I just want to show that this is possible, but I don't recommend it.) If you want to use ISO-8859-1 encoding, you can try the following:

>>>b = bytes (' \xc3\x9f\x65\x74\x61 ', ' iso-8859-1 ') >>>print (b) B ' \xc3\x83\xc2\x9feta '

If the initializer (initializer) is a string, you must provide an encoding. If the initializer is a bytes literal, you do not need to specify the encoding type: Remember, bytes literal is not a string. However, similar to strings, you can concatenate multiple bytes:

>>>b ' Hello ' b ' world ' B ' Hello World '

bytes()the method represents the binary data and the encoded text. To bytes change to str , the bytes object must be decoded (described in more detail later). Binary data is encoded using a decode() method. For example:

>>>b ' \xc3\x9f\x65\x74\x61 '. Decode () ' ßeta '

You can also read binary data directly from a file. Take a look at the following code:

>>>data = open (' Dat.txt ', ' RB '). Read () >>>print (data) # data is a string>>># content of DATA.TX T printed out here

Its function is to open the file to read a file object in binary mode and read it throughout the file.

Back to top of page

String

Python has a single string type str , which functions like the Unicode type of version 2.x. In other words, all strings are Unicode strings. And-it's also very handy for non-Latin text users-non--ascii identifiers are now also allowed. For example:

>>>césar = ["Author", "Consultant"]>>>print (César) [' Author ', ' consultant ']

In the previous version of Python, the method converts a repr() 8-bit string to ASCII. For example:

>>>repr (' é ') ' \\xc3\\xa9 ' "

Now, it returns a Unicode string:

>>>repr (' é ') ' e '

As I mentioned earlier, this string is a built-in string type.

The string object and the byte object are incompatible. If you want to get a string representation of a byte, you need to use its decode() method. Conversely, if you want to get bytes literal representation from this string, you can use the method of the string object encode() .

Back to top of page

Changes in the formatting of strings

Many Python programmers feel that the built-in operator for formatting strings % is too limited, because:

    • It is a binary operator and can accept up to two parameters.
    • In addition to formatting string parameters, all other parameters must be squeezed with a tuple (tuple) or a dictionary (dictionary).

This format is somewhat inflexible, so Python 3 introduces a new way to format strings (version 3 retains % operators and string.Template modules). String objects now have a method format() that accepts positional and keyword arguments, both of which are passed to the replacement field . The replacement field is marked by curly braces () within the string {} . The elements in the replacement field are simply called a field. Here is a simple example:

>>> "I love {0}, {1}, and {2}". Format ("Eggs", "bacon", "sausage") ' I love eggs, bacon, and sausage '

The field {0},{1} , and {2} eggs are passed bacon sausage to the format() method through positional parameters. The following example shows how to format using format() the pass-through of the keyword parameter:

>>> "I love {a}, {B}, and {C}". Format (a= "Eggs", b= "bacon", c= "sausage") ' I love eggs, bacon, and sausage '

Here is another example of a combination of positional and keyword parameters:

>>> "I love {0}, {1}, and {param}". Format ("Eggs", "bacon", param= "sausage") ' I love eggs, bacon, and sausage '

Keep in mind that placing a non-keyword argument after the keyword argument is a syntax error. To turn Yihua brackets, simply use double curly braces, as shown below:

>>> ' {{0}} '. Format ("Can ' t see me") ' {0} '

The positional parameters are can‘t see me not output, because no fields can be exported. Please note that this does not produce an error.

The new format() built-in function can format a single value. Like what:

>>>print (Format (10.0, "7.3g"))       10

In other words,g represents the general format , which outputs a fixed width value. The first value before the decimal point specifies the minimum width, and the value after the decimal point specifies the precision. The complete syntax for format specifier is beyond the scope of this article, and more information can be found in the Resources section of this article.

Back to top of page

Change of built-in Dict type

Another significant change within 3.0 is the deletion of dictionaries dict.iterkeys() , dict.itervalues() and dict.iteritems() methods. Instead, .keys() they are .values() patched and .items() can return a lightweight, set-like container object instead of a list of keys and values. The benefit is that you can perform operations on a key and item without copying it set . For example:

>>>d = {1: "Dead", 2: "Parrot"}>>>print (D.items ()) <built-in method items of Dict object at 0xb7c2468c >

Note: within Python, a set is an unordered collection of unique elements.

Here, I create a dictionary with two keys and values, and then output a d.items() value that returns an object instead of a list of values. You can set test the membership of an element like an object, such as:

>>>1 in D # Test for Membershiptrue

The following is an dict_values example of iterating over an object's entry:

>>>for values in D.items (): ...     Print (values) ... Deadparrot

However, if you do want to get a list of values, you can dict force type conversions on the returned objects. Like what:

>>>keys = List (D.keys ()) >>>print (keys) [up]

Back to top of page

New I/O meta-class

Wikipedia defines the meta-class as such, "A meta-class is a class, and its instances are classes." "In the 2nd part of this series, I will introduce this concept in detail.

Before delving into the new mechanism of I/O, it is necessary to look at the abstract base class (CLASSES,ABC). A more in-depth introduction will be provided in the 2nd part of this series.

ABC is a class that cannot be instantiated. To use ABC, subclasses must inherit from this ABC and also overwrite their abstract methods. If the prefix of a method uses @abstractmethod a modifier (decorator), then this method is an abstract method. The new ABC framework also provides @abstractproperty modifiers to define abstract properties. You can abc access this new framework by importing the standard library module. A simple example is shown in Listing 1.

Listing 1. A simple abstract base class
From ABC import abcmetaclass Simpleabstractclass (Metaclass=abcmeta):    passsimpleabstractclass.register (list) Assert Isinstance ([], Simpleabstractclass)

register()The method call accepts a class as its argument and makes this ABC a subclass of the registered class. This can be verified by invoking the statement on the last line assert . Listing 2 is another example of using modifiers.

Listing 2. An abstract base class that uses modifiers
From ABC import Abcmeta, Abstractmethodclass abstract (Metaclass=abcmeta):    @abstractmethod    def absmeth (self):        Pass Class A (abstract):    # must implement abstract method    def absmeth (self):        return 0

Once we understand ABC, we can continue to explore new I/O systems. The previous Python release lacked some important but excellent functions, such as those used for stream-like objects seek() . A stream-like object is a read() write() file-like object with and method-such as a socket or file. Python 3 has many I/O layers for stream-like objects-an original I/O layer, a buffered I/O layer, and a text I/O layer-each layer is defined by its own ABC and implementation.

Whether you open a stream or need to use a built-in open(fileName) function, but you can also call it io.open(fileName)) . This returns a buffered text file, read() and readline() returns a string (note that all strings within Python 3 are Unicode). You can also use open(fileName, ‘b‘) the open a buffered binary file. In this case, the read() bytes are returned, but readline() they are not available.

The constructor for this built-in open() function is:

Open (file,mode= "R", Buffering=none,encoding=none,errors=none,newline=none,closefd=true)

The possible patterns are:

    • r : Read
    • w : Open for write
    • a : open for Append
    • b : binary mode
    • t : text mode
    • + : opens a disk file for update
    • U : Universal line break mode

The default mode is rt to open the text mode for reading.

bufferingThe expected value of the keyword parameter is one of the following three integers to determine the buffering policy:

    • 0 : Turn off buffering
    • 1 : Row buffer
    • > 1 : full buffering (default)

The default encoding method is independent of the platform. Close the file descriptor or closefd can be True or False. If False, this file descriptor is persisted after the file is closed. If the file name does not work, it closefd must be set to True.

open()The object returned depends on the mode you are setting. Table 1 shows the return type.

Table 1. return types for different open modes
Mode return Object
Text mode TextIOWrapper
Binary BufferedReader
Write binary BufferedWriter
Append binary BufferedWriter
Read/write mode BufferedRandom

Note: text patterns can be,,, w r , and wt rt so on.

The example shown in Listing 3 opens a buffered binary stream for reading.

Listing 3. Opens a buffered binary stream for reading
>>>import io>>>f = Io.open ("Hashlib.pyo", "RB")  # Open for reading in binary mode>>>f                                 # is a BufferedReader object <io. BufferedReader object at 0xb7c2534c>>>>f.close ()                         # Close Stream

BufferedReaderobjects can access a number of useful methods, such as,,,,,,,,,, isatty peek raw readinto readline readlines seek seekable tell writable write and writelines . To see the full list, you can BufferedReader run it on the object dir() .

Back to top of page

Conclusion

Will the Python community pick up? Version 3 is still in the people's guess. Breaking backward compatibility means that two versions will be supported. Some project developers may not want to migrate their projects, even if they are using versions 2 to 3 converters. Personally, I've found that migrating from Python version 2 to 3 is just a re-understanding of a few things: it certainly won't change as strongly as migrating from Python to Java or Perl. Many changes have long been expected, such as the change in dict substance. Execution is print() much easier than executing Java System.out.println() , and learning is relatively easy, so it does bring some benefits.

I suspect that some of the posts in blogosphere will make Python supporters mistakenly think that some of these changes-such as the breaking of backwards compatibility-have devastating effects. Lambda is ready to be deleted, but has not done so, and still retains its original format. For a complete list of reserved items, please visit the Python Core development site. If you have enough spirit of exploration to delve into all of the PEP, you'll be able to get more in-depth information from it.

The next installment of this series will cover more advanced topics such as meta-class syntax, ABC, modifiers, integer literal support, base types, and exceptions.

A preliminary discussion of Python 3, part 1th: New features of Python 3

Related Article

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.