Python syntax
==
Different from is
.
==
It compares the values of two objects, is
is the address (id) compared in memory, and is
is equivalent to id(objx) == id(objy)
.
# The shallow copy operation will only copy the first level of the object being copied, and for the deeper level, it just copies its reference, as in the following example`a[2]`
# with`lst[2]`These two objects are the second layer. In fact, after the shallow copy, these two objects are still one object. Deep copy will be copied completely
# All hierarchical objects of the shell object are a true copy.
>>> from copy import copy, deepcopy
>>> lst =[1,2,[3,4]]>>> a, b =copy(lst),deepcopy(lst)>>> a,b([1,2,[3,4]],[1,2,[3,4]])>>>id(lst[2]),id(a[2]),id(b[2])(139842737414224,139842737414224,139842737414584)>>> lst[0]=10>>> a
[1,2,[3,4]]>>> b
[1,2,[3,4]]>>> lst[2][0]='test'>>> lst
[10,2,[' test',4]]>>> a
[1,2,[' test',4]]>>> b
[1,2,[3,4]]
__ init__
and __new__
.
__ init__
simply initializes some attributes of the instance, and performs some necessary custom operations that need to be created when creating an object, and has no return value. And __new__
returns the instance created by the user. This is the real instance used to create the instance, so __new__
is executed before __init__
, which is created and then initialized.
lz simply said a few points at the time:
Python2 and Python3 have different default strings, and Python3 defaults to Unicode.
raw_input()
, input()
To catch exceptions/errors, Python2 also supports except Exception, e
in addition to the following, while Python3 only supports except Exception as e
There is no xrange
in Python3, and range
is used instead. In Python3, range
returns an iterable object instead of directly returning a list like Python2.
In Python3, map
must be executed in the manner of list(map())
if it needs to be executed immediately.
In Python3, print
has been changed to a function, while in Python2, print
is a keyword. There are differences in usage.
In Python3, 3/2 == 1.5
; in Python2, 3/2 == 1
.
The above knowledge lists a few more common ones. Here is a detailed article written by http://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html.
What are the ways to connect strings?
Format character concatenation (%s
)
format
join
+
How to determine the type of an object?
type(obj)
isinstance(obj)
It’s hard to explain in one word, I recommend reading this translation of stackoverflow answer
What is the GIL in Python? Full name? Give an example to talk about its specific manifestations.
The full name of GIL is Global Interpreter Lock. Before any Python thread executes, the GIL lock must be acquired. Then, every time 100 bytecodes are executed, the interpreter automatically releases the GIL lock, allowing other threads to have a chance to execute. There are several ways to avoid this "phenomenon" and use the multi-core advantages of the operating system:
Use C language to write extensions, create native threads, and get rid of GIL, but even if it is an extension, any piece of Python code in the Python code still has GIL restrictions
Use multi-process instead of multi-thread. When using multi-process, each process has its own GIL. Therefore, there is no GIL restriction between processes. But multiple processes cannot share memory.
s ='abcd', s[2] ='e'
What is the result of the operation?
Error, string is immutable object
In Python, what algorithm is inside the sorted
function?
It is mentioned in Official Document that the Timsort algorithm is used
What kind of process is encoding?
Encoding is a process from binary to character
At that time, lz also briefly said that it can be achieved with the yield keyword, and gave a small example, and also said that user-controlled scheduling, plus some third-party frameworks, Gevent, tornado, etc., pitiful. Here is an article from Ju Ge Say Qing Daoming: What is a coroutine
What is the difference between the requests
package to create a new session
and then get
and ordinary requests.get
? (Tcp long connection)
Maintain a session, ** establish a tcp long connection**, the cookie is automatically saved, and the next request is still a session.
Variable objects: lists, dictionaries
String, number, tuple, set
Pass by reference.
Will you write tests for your project? Which libraries are used by which methods?
I just talked about using unitest... you need to find the answer yourself.
Please create a new list and tuple with only one element 1
.
lst = [1]
tup = (1,)
>>> def foo(a, b=[1,2]):print(b)
b.append(a)print(b)>>> val =4>>>foo(val)
# [1,2]
# [1,2,4]>>> foo(val)
# [1,2,4]
# [1,2,4,4]
# Here you can see that the second time the function is executed, the value of the default parameter b has become`[1, 2, 4]`The reason is that the default parameter is only in the first
# It will be initialized when executed once, and will be used by default later**This object after initialization(Quote)**, But here b is a mutable object,
# The added element is still the previous object, so the reference has not changed, but the value has changed.
In fact, in the Flask class, route
can be simply understood as just using the corresponding routing rule as the key, and the decorated view function as the value, stored in the werkzeug.routing.Map
object (it can be regarded as similar to a dictionary Data structure). Here is source code, which is easier to understand. This is a [note] written before (https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fx1ah%2FBlog%2Fissues%2F4)
Flask has the advantages of being lightweight, flexible, highly customizable, and plug-in. The disadvantage is that it is too light, the function must be realized through a third-party plug-in, the quality of the plug-in varies, and the subsequent maintenance cannot be fully guaranteed.
These points are just personal opinions. For more detailed standards, you need to find the answers yourself.
Here is an explanation from Wikipedia. WSGI is a universal standard. By complying with this standard, we can make our web framework more versatile and easier to write.
Both uwsgi and Nginx are Web Servers. The difference is that Nginx is responsible for external network requests --- (conversion) --> internal network requests, while uwsgi is responsible for internal network requests -> Python Web programs.
What is the difference between nginx and Apache? (Reference interview_python)
The advantages of nginx over apache:
Lightweight, it also takes up less memory and resources than Apache
Anti-concurrency, nginx processes requests asynchronously and non-blocking, and supports more concurrent connections, while apache is blocking. Under high concurrency, nginx can maintain low resources, low consumption and high performance
Simple configuration
Highly modular design, relatively simple to write modules
Active community
The advantages of apache over nginx:
rewrite, more powerful than nginx's rewrite
There are so many modules, you can find everything you can think of
Fewer bugs, relatively more bugs in nginx
Super stable
Which mode of uWSGI did you use to deploy your Python project?
Default mode
The possibility of this question is extremely small, please refer to uwsgi document for more detailed information
# coding: utf-8from collections import deque
classBNode:"""Binary Tree Node"""
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def level_traverse(binary_tree):"""Hierarchical traversal binary tree"""
stack =deque([binary_tree])while stack:
top = stack.popleft()print(top.value)if top.left:
stack.append(top.left)if top.right:
stack.append(top.right)if __name__ =="__main__":
b_tree =BNode(1,BNode(2,BNode(4,BNode(5,BNode(7)))),BNode(3,BNode(6, right=BNode(8))))level_traverse(b_tree)
How does an unbalanced binary number become a balanced binary number?
Reference [AVL Balanced Binary Tree Detailed Explanation and Implementation] (https://links.jianshu.com/go?to=https%3A%2F%2Fsegmentfault.com%2Fa%2F1190000006123188)
First, middle, and later traverse binary numbers. What is a perfect binary number?
Complete binary tree: A binary tree with n nodes in depth k, if and only if each node in it can correspond to a full binary tree of the same depth k, and nodes with sequence numbers 1 to n, it is called "complete Binary Tree". (From Wikipedia)
First order: root first, left then right
Middle order: left, then center, then right
Post-order: left and right then root
How to judge whether two singly linked lists intersect at a node, including X-type, Y-type, and V-type.
Type X cannot exist. There are no two different successors for a singly linked list node.
# There are V-type and Y-type. If they cross, the last node must be the same, so the reverse traversal is performed directly from the last node.
# Reverse singly linked list
def reverse_single_link_lst(link_lst):if not link_lst:return link_lst
pre = link_lst
cur = link_lst.next
pre.next = None
while cur:
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre
# Find intersection
def point(node_a, node_b):if node_a is None or node_b is None:return None
next_a, next_b = node_a, node_b
while next_a or next_b:if next_a.val == next_b.val:if next_a.next and next_b.next and(next_a.next.val == next_b.next.val):
next_a, next_b = next_a.next, next_b.next
continuereturn next_a.val
next_a, next_b = next_a.next, next_b.next
return None
# Construct a singly linked list
classNode(object):
def __init__(self, value, next=None):
self.val = value
self.next = next
a =ListNode(1,ListNode(2,ListNode(3,ListNode(4,ListNode(5)))))
b =ListNode(7,ListNode(9,ListNode(4,ListNode(5))))
ra =reverse_single_link_lst(a)
rb =reverse_single_link_lst(b)point(ra, rb)
# output:
# 4
How to judge whether two singly linked lists are the same linked list.
Just judge the first node directly.
The singly linked list is reversed.
See the reverse_single_link_lst()
function in the judgment cross-linked list above.
[ Heap)(https://links.jianshu.com/go?to=https%3A%2F%2Fzh.wikipedia.org%2Fwiki%2F%25E5%25A0%2586_%28%25E6%2595%25B0%25E6%258D %25AE%25E7%25BB%2593%25E6%259E%2584%29), Stack, Queue
# Quick sort, lz was more complicated to write at the time, but it was the most common way of writing (the tension caused a few small bugs), as follows
def quick_sort(lst, start, stop):if start < stop:
i, j, x = start, stop, lst[start]while i < j:while(i < j)and(lst[j]> x):
j -=1if(i < j):
lst[i]= lst[j]
i +=1while(i < j)and(lst[i]< x):
i +=1if(i < j):
lst[j]= lst[i]
j -=1
lst[i]= x
quick_sort(lst, start, i-1)quick_sort(lst, i+1, stop)return lst
After that, the interviewer akun gave a very concise notation, three-way multiplexing, the address is in Gist
def qsort(alist):"""
quick sort(easy way, but more memory)
test: python -m doctest qsort.py
>>> import math
>>> import random
>>> size =100>>> alist =[random.randint(0, size *10)for i inrange(size)]>>> qlist =qsort(alist)>>> alist.sort()>>> assert qlist == alist
"""
iflen(alist)<=1:return alist
key = alist[0]
left_list, middle_list, right_list =[],[],[][{i < key: left_list, i == key: middle_list, i > key: right_list}[
True
]. append(i)for i in alist]returnqsort(left_list)+ middle_list +qsort(right_list)
What is the principle of indexing? What are the advantages and disadvantages?
Reference [The realization principle of database index] (https://links.jianshu.com/go?to=http%3A%2F%2Fblog.csdn.net%2Fkennyrose%2Farticle%2Fdetails%2F7532032)
What are optimistic locking and pessimistic locking?
Why did you choose Redis instead of MongoDB or other? (One project uses Redis)
What are the differences between redis, memcache, and mongoDB?
What is the difference between SQL and NoSQL?
What happened from the browser input of the URL to the completion of the page rendering?
Here It is very detailed, depending on the post, I answered The focus is different. As in the Web, you can focus on the process of nginx -> uwsgi -> Python -> uwsgi -> nginx (WSGI standard)
[ Three handshake and four wave of hands in the TCP protocol (illustration))(https://links.jianshu.com/go?to=http%3A%2F%2Fblog.csdn.net%2Fwhuslei%2Farticle%2Fdetails%2F6667471)
Why is it a three-way handshake? Can't it twice?
[ Why is a "three-way handshake" required during TCP connection establishment](https://links.jianshu.com/go?to=http%3A%2F%2Fwww.cnblogs.com%2Ftechzi%2Farchive%2F2011%2F10%2F18%2F2216751 .html)
TCP (Transport Layer)
Advantages: TCP is connection-oriented, reliable, and stable. A connection needs to be established before data transmission, so there are three-way handshake and four waves, as well as congestion control, retransmission, etc.
Disadvantages: slow, occupy system resources, have confirmation mechanism, three-way handshake, so easy to be attacked, DDos
UDP
Advantages: fast, stateless transfer protocol
Disadvantages: unstable, unreliable, easy to lose packets
Talk about your understanding of SQL injection, XSS, CSRF. And how to prevent it.
About XSS (Cross Site Scripting Attack) and CSRF (Cross Site Request Forgery)
[ SQL injection](https://links.jianshu.com/go?to=https%3A%2F%2Fzh.m.wikipedia.org%2Fzh-hans%2FSQL%25E8%25B3%2587%25E6%2596%2599% 25E9%259A%25B1%25E7%25A2%25BC%25E6%2594%25BB%25E6%2593%258A), now most of them use ORM, and parameterized queries, rarely appear again.
Find the protocol of the host IP based on the domain name.
Which layer of the seven-layer model does HTTP work? What layer is DNS? What about TCP and IP?
HTTP, DNS application layer, TCP transport layer, IP network layer.
[ Status code)(https://links.jianshu.com/go?to=https%3A%2F%2Fzh.wikipedia.org%2Fwiki%2FHTTP%25E7%258A%25B6%25E6%2580%2581%25E7%25A0% 2581), here only need to talk about it, in terms of 1××, 2××, 3××, there is no need to go down to each status code.
[ HTTP request method](https://links.jianshu.com/go?to=https%3A%2F%2Fzh.wikipedia.org%2Fwiki%2F%25E8%25B6%2585%25E6%2596%2587%25E6%259C %25AC%25E4%25BC%25A0%25E8%25BE%2593%25E5%258D%258F%25E8%25AE%25AE%23.E8.AF.B7.E6.B1.82.E6.96.B9.E6.B3 .95)
In essence, GET and POST are just different sending mechanisms.
What is the difference between HTTP and HTTPS?
HTTPS = HTTP + SSL
HTTP uses port 80 by default, and HTTPS uses port 443.
[ More detailed](https://links.jianshu.com/go?to=https%3A%2F%2Fzh.wikipedia.org%2Fwiki%2F%25E8%25B6%2585%25E6%2596%2587%25E6%259C% 25AC%25E4%25BC%25A0%25E8%25BE%2593%25E5%25AE%2589%25E5%2585%25A8%25E5%258D%258F%25E8%25AE%25AE%23.E4.B8.8EHTTP.E7.9A. 84.E5.B7.AE.E5.BC.82)
Tell me about the fields in the header information of the HTTP packet you know.
Just grab the bag and you will know it, so I won’t talk about it~
Host
field in the HTTP packet header information? *Represents the host name of the current requesting server
Tell me about which fields you know in cookies.
What is Session?
In the process of writing a crawler, if you encounter a situation that needs to load js, how do you deal with it?
Selenium,PhantomJS
What is the difference between ordinary anonymous proxy and high hidden proxy?
Requests from ordinary anonymous agents can see the real IP on the server side, while high hidden agents cannot see the real IP on the server side, only the proxy server IP.
What anti-climbing measures do you know?
Add proxy, add head, crawl speed control, random UA, and simulate the clicking habits of real users to request.
match
function and the search
function in the regular module re
in Python? for example.retry
decorator, use it as follows:# You can specify the number of retries until the function returns the correct result.
@ retry(retries=3)
def func(*args,**kw):try:
# some action
return True
except:return False
It can be written like this,
from functools import wraps
def retry(retries=3):
def timesf(func):
@ wraps(func)
def wrap(*args,**kw):
i =0
status = True
while status and i < times:
status =func(*args,**kw)
i +=1return status
return wrap
return timesf
hotel ID + number of occurrences
(Finally mentioned other ideas, such as file slices, bitmaps, etc.)xxxx-xx-xx
, pay attention to the year, size and month. Library functions are not allowed. Try to implement the lower layer as possible. (handwriting)Recommended Posts