Source | novice learning Python

NO.1

Python is so good

Tell me about its characteristics

The key features

Python is an interpreted language, which means that, unlike C or C++, Python does not need to be compiled before it can be run. It’s explaining as it goes along.

Python is dynamically typed, which means that when you declare them or something like that, you don’t need to declare the type of a variable. You could have x=1, and then x=” ABC “without error.

Python is well suited for object-oriented programming because it allows defining classes as well as composition and inheritance. Python has no access modifiers (such as C ++ public, private).

In Python functions are first-class objects, which means they can be created dynamically at run time, can be assigned to variables or passed to functions as arguments, and can be returned by functions

Python code is easy to learn and fast to develop, but generally runs slower than compiled languages. Fortunately, Python allows for the inclusion of C-based extensions, so bottlenecks can be optimized away, for example, the Numpy package is a good example, it’s very fast because a lot of the operations it does are written in C at the bottom!

NO.2

Assignment in Python

Difference between shallow copy and deep copy

Depth copy difference

1). Object assignment

Assignment of objects in Python is actually a simple object reference meaning that when you create an object and then copy it to another variable, Python doesn’t copy the object, it copies the reference to the object.

2). A shallow copy

Copy. Copy () is usually used to make a shallow copy of an object. It copies the object but still uses the original reference for the elements in the object.

[2,3,4]; [2,3,4]; [2,3,4]; [2,3,4]; [1]; [2,3,4]; [2,3,4]

3). Deep copy

Copy. Deepcopy () is used for deepcopy. It copies a container object and all its elements (including its children)

After a deep copy of the list a, b copies the object of a, but the [2,3,4] in b and the [2,3,4] in a actually refer to different memory addresses.

A [1]. Append (5) the print print (a) (b) > > [1, [2, 3, 4, 5]] # because it is a deep copy, changed a [1], [1] b content does not change the [[2, 3, 4]] 1,

NO.3

In Python = =

The difference with IS

= = and is

Difference between == and is in Python

Is checks whether the object identifier is consistent, and == checks whether the contents of two objects are equal!

X is y is equal to id(x)==id(y).

== checks whether the contents of two objects are equal, calling the object’s internal __eq__().

NO.4

How do threads work in Python

In the implementation

Multithreading problem

How are threads implemented in Python

Python has a package called Threading that allows you to use multiple threads to speed up your code. But Python has a construct called the Global Interpreter Lock (GIL). GIL ensures that only one ‘thread’ can execute at any time.

The thread gets the GIL, does some work, and then passes the GIL to the next thread. This happens very quickly, so to the human eye it might look like your threads are executing in parallel, but they’re actually just taking turns using the same CPU kernel. Therefore, GIL’s presence prevents multithreading in Python from really taking advantage of multi-core to improve performance.

For IO intensive operations, the GIL is released while waiting for the operating system to return. For example, crawler can use multithreading to speed up the response time of waiting servers! But for CPU-intensive operations, it can only be accelerated through a Multiprocess.

NO.5

In Python

Monkey patch

The monkey patch

What is the monkey patch in Python?

Consider the following example:

>>f()

Monkey patch:

Python is a very Pythonic use, which means functions can be assigned as variables in Python, and we can dynamically replace modules at run time, a trick known as monkey patching! We dynamically change the output by reassigning myclass. f.

NO.6

In Python

Negative index

Negative index

What is the negative number index used for in Python?

Sequences in Python are indexed and consist of positive and negative numbers. Positive numbers use ‘0’ as the first index and ‘1’ as the second index.

Negative indexes start with ‘-1’ and represent the last index in the sequence, ‘-2’ as the penultimate index, and the sequence moves forward like a positive number.

Negative indexes can also be used for very convenient slicing, such as:

s=’abcdedf’

print (s[1:-1])

>>bcded

NO.7

Class new

And init

New/init

Tell me the difference between __new__ and __init__

__init__ is the initializer, and __new__ is the real constructor. Only new classes that inherit from Object have __new__.

__new__ must have at least one argument, CLS, which represents the class to be instantiated. This argument is automatically provided by the Python interpreter at instantiation time. __new__ must have a return value, which returns the instantiated instance.

__init__ takes an argument, self, which is an instance of the __new__ returned by running __new__ first and then __init__.

__init__ does some other initialization on top of __new__. __init__ does not return a value.

NO.8

In Python

parameter

* args and * * kwargs

Use of *args, **kwargs in Python

* Args is a mutable argument, generally used to indicate that we are not sure how many arguments will be passed to a function, or if we want to pass them as lists or tuples.

**kwars is a variable keyword argument, used when we don’t know how many keyword arguments will be passed to a function, or when we want to use a dictionary as a keyword argument

Note: *args and **kwargs can be in the function definition together, but *args must precede **kwargs.

Why there are no overloaded functions in Python?

End