Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the skill of Python extension module?

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Python extension module skills are what", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Python extension module skills are what"!

Python extension module open source, portable to a variety of operating systems, as long as avoid the use of specific operating system-dependent features, Python programs can run on a variety of platforms without modification, so convenient language will certainly surpass the current mainstream programming language.

Strings can be easily read into or written out of files. Reading in values is a bit tricky because read() always returns a string, and you pass that string to a function like string.atoi(). Convert a string like '123' to the corresponding integer value of 123. However, when you want to save more complex data types such as lists, dictionaries, or class instances, reading and writing are much more complex.

Python is designed so that programmers don't have to write code to debug complex data types repeatedly, and it provides a standard module called pickle. This amazing module can convert almost any Python object into a string representation.

This process is called pickling, and restoring objects from their string representation is called restoring. Between curing and uncuring, string representations of objects can be stored in files or data, or even transmitted to remote computers over network connections. If you have an object x and a writable file object f, the simplest way to pickle the object is to write the following line of code: To recover the object, if the file is already open for reading, the file object name remains f.

(There are other uses for pickling and restoring, multiple objects can be pickled, and data can be written to no file, see the library reference manual for details). Pickles are the standard way to save Python extension modules and call them by other programs or when the same program runs again later. The technical term for this practice is "persistent objects." Because pickle is widely used, many authors of Python extension modules take care that newly added data types such as matrices can be pickled and restored correctly.

We often need to control the output format, not just display values separated by spaces. There are two ways to control the output format: One way is to do your own string processing. Any conceivable format can be produced with string fragmentation and merging operations. The standard module string contains useful operations such as padding strings to a specified column width, as mentioned later.

There is also a function string.zfill() that fills in zeros to the left of the value. This function can handle situations with plus and minus signs:

>>> string.zfill('12', 5) '00012' >>> string.zfill('-3.14', 7) '-003.14' >>> string.zfill('3.14159265359', 5) '3.14159265359' The % operator is used as follows: >>> import math >>> print 'The value of PI is approximately %5.3f. ' % math.pi The value of PI is approximately 3.142. If there are multiple values that can be given in an order table, then there must be multiple formats in the format string, such as: >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} >>> for name, phone in table.items(): ... print'%-10s ==> d' % (name, phone) ... Jack ==> 4098 Dcab ==> 8637678 Sjoerd ==> 4127

The *** line of the Python extension module error message shows what happened. Exceptions come in different types that are displayed as part of the error message: the types of errors in the example above are ZeroDivisionError, NameError, and TypeError. The string displayed as an exception type is the built-in name of the exception that occurred. This is true for all built-in exceptions, but not necessarily for user-defined exceptions (users *** can abide by such conventions). Standard exception names are built-in identifiers (not reserved keywords).

The rest of this line is the details of the error, the interpretation of which depends on the exception type. The section preceding the error message shows the context in which the error occurred, in the form of a stack trace. Typically this contains a stack trace listing source lines; however, it does not display lines read from standard input.

At this point, I believe that everyone has a deeper understanding of "Python extension module skills", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report