程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python eight-part text

編輯:Python

Language features of py: dynamic and strong typing
static/dynamic: compiling/runtime determining variable types

Weak/strong typing: Implicit conversions will/will not occur

The advantages and disadvantages of py as a back-end language
The language is simple and flexible, and the development efficiency is high

Glue language, many wheels, and a mature web framework like Django

Inefficient execution, performance is not as good as other languages

Dynamic languages, there is no auto-completion without type declaration, and many problems are discovered after running

What is duck type?
Duck type pays more attention to the behavior of the object, as long as it implements a certain interface method, it doesn't care what type

For example, class instance objects that define the __iter__ magic method can be iterated with for

What is a monkey patch?
A monkey patch is a runtime replacement of an object, essentially a reassignment of the object

The difference between py3 and py2
print is a function in py3, just a keyword in py2
the default encoding of py3 files is utf8, and the default encoding of py2 files is ascii
str of py3is a unicode string, and py2's str is bytes
py3's range() returns an iterable object, py2's range() returns a list, xrange() returns an iterable object,
py3's division returnsThe division of float, py2 returns int
Mutable and immutable objects
Mutable objects: list, dict, set
Immutable objects: bool, int, float, tuple, str…
FunctionsPassing *args, **kwargs
is used to handle variable parameters. After receiving the parameters, args will become a tuple, and kwargs will become a dict

When do I need to catch exceptions?
When Django's ORM framework operates the database, it may be abnormal to obtain data, update data, etc.
When socket communication, the recv() method may be caused by the sudden interruption of the connection by the other partyExceptions
What is the CPython GIL?
GIL, Global Interpreter Lock, or Global Interpreter Lock

The GIL was introduced because CPython's memory management is not thread-safe,

In order to protect the access to python objects under multi-threading, each thread needs to obtain the GIL first during the execution process to ensure that only one thread is executing code at the same time

GIL makes python's multi-threading unable to fully utilize the performance of multi-core CPUs, which has a greater impact on CPU-intensive programs

What is a generator?
A generator is an iterable object that can suspend and maintain its current state

The generator will stop executing when it encounters yield, and will continue to execute after calling next() or send()

There are two ways to define a generator, one is generator comprehension, the other is to add a yield statement to a normal function and instantiate it

Shallow copy and deep copy
The shallow copy is an independent object, but its sub-objects are still sub-objects in the original object

Deep copy will recursively copy each sub-object in the original object, so the copied object and the original object are not related to each other.

The difference between iterator and iterable object
Iterable object class, you must customize the __iter__() magic method, the instantiated objects of the range and list classes are all iterable objects

Iterator class, you must customize the __iter__() and __next__() magic methods, use the iter() function to create an iterator of iterable objects

Closure
A closure is a nested function, its inner function uses the variables or parameters of the outer function, and its outer function returns the inner function

The variables in the external function can be saved and will not be destroyed when the external function is called

Python garbage collection mechanism
Reference counting is the main, mark removal and generational collection are supplemented

The reference counting mechanism is like this

When the object is created, referenced, passed as a parameter, stored in the container, the reference count +1

When the object goes out of scope, the reference points to another object, del, removes it from the container, and the reference count is -1

When the reference count drops to 0, python will automatically reclaim the memory space where the object is located,

However, reference counting cannot solve the problem of circular references, so mark removal and generational recycling mechanisms are introduced

The role of async and await
async: declare a function as an asynchronous function, as long as there is await in the function, it must be declared as async

await: When used with asyncio.sleep(), the coroutine will be switched, and the following statement will be executed after switching back.

Built-in data structures and algorithms
Built-in data structures: list, dict, tuple, set
Built-in algorithms: sorted, max
collections module
collections module provides some useful container dataType, which are commonly used: namedtuple, deque, Counter, OrderedDict, defaultdict

Why is the time complexity of dict lookup O(1)?
The bottom layer of dict is a hash table, which is similar to an array in C language and can achieve random access by index

But the key of dict is not necessarily an integer, it needs to be converted into an index through a hash function first, and then through a remainder operation

The underlying structure of list tuple
The underlying structure of both list and tuple is a sequential list structure

The bottom layer of the list is a variable array, and the array stores the pointers to the element objects

The underlying structure of the set
Hash table, the key is the element, and the value is empty

The difference between class method and static method
The first parameter of class method is cls, which can access class attributes and class methods

The static method is the same as the ordinary function, but it is placed in the class and must be called through the class or instance, but it cannot access the properties and methods of the class and instance

What is a decorator?
A decorator is a closure function that accepts a function as a parameter

It can add extra functionality to a function without modifying its internal source code

import timedef calc_time(func):def inner():t1 = time.time()func()t2 = time.time()print('cost time: {}s'.format(t2-t1))return inner

What is a metaclass? Usage scenarios
A metaclass is a class that creates a class, and both type and classes that inherit from type are metaclasses

Function: You can add custom functions when the class is defined (new, init) and when the class is instantiated (call)

Usage scenario: A class created in the ORM framework represents a table in the database, but when defining this class, in order to unify all the class attributes in it need to be changed to lowercase. At this time, the new method needs to be rewritten with a metaclass.Convert the key in the attrs dictionary to lowercase


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved