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 are the design methods of Python

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

Share

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

This article mainly introduces "what are the Python design methods". In the daily operation, I believe many people have doubts about the Python design methods. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "what are the Python design methods?" Next, please follow the editor to study!

15. Why doesn't CPython use a more traditional garbage collection scheme?

First of all, this is not a C standard feature and therefore cannot be portable. Yes, we know the Boehm GC library. It contains assembly code for most common platforms (but not all), and although it is basically transparent, it is not completely transparent; patches are needed for Python to use it.)

When Python is embedded in other applications, traditional GC also becomes a problem. In a stand-alone Python, the standard malloc () and free () can be replaced with the version provided by the GC library, and applications embedded in Python may want to replace malloc () and free () with it themselves, but may not need Python. Now, CPython can correctly implement malloc () and free ().

16. Why not release all memory when CPython exits?

When Python exits, objects referenced from the global namespace or Python module are not always released. If there are circular references, it can happen that some of the memory allocated by the C library is also impossible to free (for example, tools like Purify complain about this). However, Python cleans up memory on exit and attempts to destroy each object.

If you want to force Python to delete something when it is released, run a function using the atexit module to force it to be deleted.

17. Why are there separate tuples and list data types?

Although lists and tuples are similar in many ways, they are usually used in completely different ways. Tuples can be thought of as similar to Pascal records or C structures; they are small collections of related data that can be different types of data and can be manipulated as a group. For example, Cartesian coordinates are appropriately represented as tuples of two or three numbers.

On the other hand, lists are more like arrays in other languages. They tend to hold a different number of objects, all of which are of the same type and operate one by one. For example, os.listdir ('.') Returns a list of strings representing files in the current directory. If one or two files are added to the directory, the functions that operate on this output are usually uninterrupted.

Tuples are immutable, which means that once a tuple is created, you cannot replace any of its elements with new values. The list is variable, which means that you can always change the elements of the list. Only immutable elements can be used as key for dictionaries, so only tuples and non-lists can be used as key.

18. How is the list implemented in CPython?

CPython's list is actually a variable-length array, not an lisp-style linked list. The implementation uses a contiguous array of references to other objects and retains pointers to that array and its length in the column header structure.

This makes the operating cost of the index list a [I] independent of the size of the list or the value of the index.

When you add or insert an item, the reference array is resized. Some clever methods are used to improve the performance of repeated additions; when the array must grow, some extra space is allocated so that there is no need to actually resize in the next few times.

19. How are dictionaries implemented in CPython?

The dictionary of CPython is implemented as a resizable hash table. This provides better performance for lookup (by far the most common operation) and is easier to implement in most cases than a B-tree.

The dictionary works by using the hash () built-in function to calculate the hash code for each key stored in the dictionary. The hash code varies greatly depending on the key and the seed of each process; for example, "Python" has a hash value of-539294296, while "python" (a string with different bits) has a hash value of 1142331976. The hash code is then used to calculate the location in the internal array where the value will be stored. Suppose that all the keys you store have different hash values, which means that the dictionary needs a constant time-- O (1)-- in Big-O notation-- to retrieve a key.

20. Why must the dictionary key be immutable?

The hash table implementation of the dictionary uses the hash value calculated from the key value to find the key. If the key is a mutable object, its value may change, so its hash value will also change. However, because whoever changes the key object cannot determine whether it is used as a dictionary key value, the entry cannot be modified in the dictionary. Then, when you try to find the same object in the dictionary, you will not be able to find it because its hash value is different. If you try to find the old value, you won't find it, because the values of the objects found in the hash table will be different.

If you want a dictionary indexed by a list, simply convert the list to a tuple; use the function tuple (L) to create a tuple with the same entries as list L. Tuples are immutable and can be used as dictionary keys.

Some unacceptable solutions have been proposed:

Hashes are listed by their address (object ID). This doesn't work, because if you construct a new list with the same values, it won't be found; for example:

Mydict = {[1,2]: '12'} print (mydict [[1,2]])

A KeyError exception is thrown because the id of [1,2] used in the second line is different from the id in the first line. In other words, you should use = = to compare dictionary keys instead of using is.

Copy when using a list as a key. This is useless, because a list as a mutable object can contain a reference to itself, and then the copied code will enter an infinite loop.

Allow lists as keys, but tell the user not to modify them. When you accidentally forget or modify the list, this will result in a class of errors in the program that are difficult to track. It also invalidates an important dictionary invariant: each value in d.keys () can be used as a dictionary key.

When a list is used as a dictionary key, it should be marked as read-only. The problem is that it's not just a top-level object that can change its value; you can use a tuple that contains a list as a key. Associating anything as a key to a dictionary requires marking all objects reachable from there as read-only-and self-referencing objects can cause an infinite loop.

If necessary, you can use the following methods to solve this problem, but you can use it at your own risk: you can wrap a variable structure in a class instance with both _ _ eq__ () and _ _ hash__ () methods. You must then ensure that the hashes of all such wrapper objects that reside in dictionaries (or other hash-based structures) remain fixed when the objects are in dictionaries (or other structures).

Class ListWrapper: def _ init__ (self, the_list): self.the_list = the_list def _ eq__ (self, other): return self.the_list = = other.the_list def _ hash__ (self): l = self.the_list result = 98767-len (l) * 555 for I, el in enumerate (l): try: result = result + (hash (el)% 9999999) * 1001 + i except Exception: result = (result% 7777777) + I * 333return result

Note that the hash calculation is complicated by the possibility that some members of the list may be unavailable and the possibility of arithmetic overflow.

In addition, this must always be the case, if o1 = = o2 (that is, o1.roomeqcharacters _ (O2) is True), then hash (o1) = = hash (O2) ``(that is, ``o1.roomroomhashcards _ () = o2.roomroomhashcards _ ()), regardless of whether the object is in the dictionary or not. If you can't meet these limitations, dictionaries and other hash-based structures will go wrong.

For ListWrapper, the wrapper list cannot be changed to avoid exceptions as long as the wrapper object is in the dictionary. Don't do this unless you are prepared to seriously consider your needs and the consequences of meeting them incorrectly. Please pay attention.

21. Why didn't list.sort () return a sorted list?

In cases where performance is important, it would be a waste to copy a list just for sorting purposes. Therefore, list.sort () sorts the list appropriately. To remind you of this fact, it does not return a sorted list. This way, when you need a sorted copy, but you also need to keep an unsorted version, the list will not be accidentally overwritten.

Use the built-in sorted () function if you want to return a new list. This function creates a new list from the provided iterable list, sorts it, and returns it. For example, here is how to iterate through the dictionary and sort by keys:

For key in sorted (mydict):... # do whatever with myopia [key].

twenty-two。 How to specify and implement interface specifications in Python?

The module interface specification provided by C++ and Java describes the prototype of the methods and functions of the module. Many people believe that compile-time enforcement of interface specifications helps build large programs.

Python 2.6adds an abc module that allows the definition of an abstract base class (ABCs). You can then use isinstance () and issubclass () to check whether the instance or class implements a specific ABC. The collections.abc module defines a set of useful ABCs such as Iterable, Container, and MutableMapping

For Python, many of the benefits of interface specifications can be achieved by implementing appropriate testing procedures for components. There is also a tool, PyChecker, that can be used to find problems caused by subclassing.

A good module test suite can not only provide regression testing, but also serve as module interface specifications and a set of examples. Many Python modules can be run as scripts to provide simple "self-testing". Even modules that use complex external interfaces can often be isolated and tested using a simple "stub" simulation of the external interface. You can use doctest and unittest modules or third-party test frameworks to construct a detailed test suite to run each line of code in the module.

Proper testing procedures can help build large and complex applications and interface specifications in Python. In fact, it might be better because the interface specification cannot test some properties of the program. For example, the append () method adds a new element to the end of some internal list; the interface specification cannot test whether your append () implementation does this correctly, but it is easy to check this property in the test suite.

Writing test suites is very useful, and you may want to design your code to make it easy to test. An increasingly popular technique is test-oriented development, which requires that parts of the test suite be written before any actual code is written. Of course, Python allows you to act hastily and not write test cases at all.

23. Why is there no goto?

You can use exception trapping to provide a "goto structure" and even work across function calls. Many people think that exception trapping can easily simulate all the reasonable uses of the "go" or "goto" structures of Ccent Fortran and other languages. For example:

Class label (Exception): pass # declare a labeltry:... If condition: raise label () # goto label... except label: # where to goto pass...

But you are not allowed to jump in the middle of the loop, which is often considered an abuse of goto. Use it with caution.

24. Why can't the original string (r-strings) end with a backslash?

More precisely, they cannot end with an odd number of backslashes: the unpaired backslash at the end escapes the closing quote character, leaving the unfinished string.

The original string is designed to facilitate the creation of input by processors (mainly regular expression engines) that want to perform their own backslash escape processing. Such processors treat mismatched trailing backslashes as errors, so the original string is not allowed to do so. In turn, allows strings to be escaped by using quotation mark characters to escape the backslash. These rules work well when r-string is used for their intended purposes.

If you are trying to build a Windows pathname, note that all Windows system calls use a forward slash:

F = open ("/ mydir/file.txt") # works fine!

If you are trying to build a pathname for the DOS command, try the following example

Dir = r "\ this\ is\ my\ dos\ dir"\" dir = r "\ this\ is\ my\ dos\ dir\" [:-1] dir = "\\ this\\ is\ my\\ dos\\ dir\\"

25. Why doesn't Python have a "with" statement for attribute assignment?

Python has a 'with' statement that encapsulates the execution of the block and invokes the code at the entry and exit of the block. Some languages are structured like this:

With obj: a = 1 # equivalent to obj.a = 1 total = total + 1 # obj.total = obj.total + 1

In Python, such a structure is ambiguous.

Other languages, such as ObjectPascal, Delphi, and C++, use static typing, so you can know unequivocally which members are assigned. This is the point of static typing-- the compiler always knows the scope of each variable at compile time.

Python uses dynamic typing. It is impossible to know which property is referenced at run time in advance. You can dynamically add or remove member attributes from an object. This makes it impossible to know what attributes are referenced by simple reading: local, global, or member attributes?

For example, use the following incomplete code snippets:

Def foo (a): with a: print (x)

This code snippet assumes that "a" must have a member attribute named "x". However, the interpreter is not told this in Python. Suppose "a" is an integer, what happens? If there is a global variable named "x", will it be used in the with block? As you can see, the dynamic nature of Python makes this choice more difficult.

However, Python can easily realize the main benefits of "with" and similar language features (reducing the amount of code) through assignments. Instead of:

Function (args) .mydict [index] [index] .a = 21function (args) .mydict [index] [index] .b = 42function (args) .mydict [index] [index] .c = 63

It is written as follows:

Ref = function (args) .mydict [index] [index] ref.a = 21ref.b = 42ref.c = 63

This also has the side effect of improving execution speed, because Python resolves name bindings at run time, while the second version only needs to perform the resolution once.

twenty-six。 Why do if/while/def/class statements need colons?

Colons are mainly used to enhance readability (one of the results of ABC language experiments). Consider this:

If a = b print (a)

Vs.

If a = b: print (a)

Note that the second method is slightly easier. Notice further how the colon is set in this example of the FAQ solution; this is the standard usage in English.

Another secondary reason is that colons make it easier for editors with syntax highlighting to work; they can look for colons to determine when indentation is needed without having to parse the program text in more detail.

twenty-seven。 Why does Python allow commas at the end of lists and tuples?

Python allows you to add a trailing comma at the end of lists, tuples, and dictionaries:

[1, 2, 3,] ('await,' baked, 'caged,) d = {"A": [1,5], "B": [6,7], # last trailing comma is optional but good style}

There are several reasons to allow this.

If the literal values of a list, tuple, or dictionary are distributed across multiple lines, it is easier to add more elements because you do not have to remember to add a comma in the previous line. These lines can also be reordered without syntax errors.

Accidentally omitting commas can lead to errors that are difficult to diagnose. For example:

X = ["fee", "fie"foo", "fum"]

This list appears to have four elements, but it actually contains three: "fee", "fiefoo" and "fum". Always add a comma to avoid the source of this error.

Allowing trailing commas can also make programming code easier to generate.

At this point, the study of "what are the Python design methods" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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