Some questions asked in the python back-end interview

1, python related issues

1. Q: what's the difference between python2 and python3?

code:

The default encoding of Python 2 is asscii, which is one of the reasons why we often encounter encoding problems in Python 2. As for why asscii is used as the default encoding, the reason is that Unicode did not appear when Python was born. Python 3 uses UTF-8 as the default encoding by default, so you no longer need to write # coding=utf-8 at the top of the file.

print:

The most frequently used statement in program debugging may be print. In Python 2, print is a statement, while in Python 3, it exists as a function.

character string:

String is one of the biggest changes, which minimizes coding problems. In Python 2, there are two types of strings, one is unicode and the other is str. the former represents text string and the latter represents byte sequence. However, there is no obvious boundary between the two. Developers also feel very confused and do not understand the causes of coding errors. However, in Python 3, the two are strictly distinguished, with STR representing string and byte representing byte sequence respectively, Any data that needs to be written into text or transmitted over the network only receives byte sequences, which prevents the problem of coding errors from the source.

True and False:

True and False are two global variables (names) in Python 2, which correspond to 1 and 0 respectively in value

Python 3 corrects this defect. True and False become two keywords, which always point to two fixed objects and are not allowed to be re assigned.

Iterator:

In Python 2, many built-in functions and methods that return list objects are changed to return objects similar to iterators in Python 3, because the lazy loading characteristics of iterators make it more efficient to operate big data. The range and xrange functions in Python 2 are combined into range. If they are compatible with both 2 and 3

nonlocal:

We all know that in Python 2, you can use the keyword global to declare a variable as a global variable in the function, but in nested functions, it is impossible to declare a variable as a non local variable. In Python 3, the keyword nonlcoal is added to make it possible to declare a non local variable.

Reprint: https://www.zhihu.com/question/19698598

Class:

  • New classes are inherited from object, and classical classes do not need it.
  • The MRO(method resolution order) algorithm of the new class adopts C3 algorithm for breadth first search, while the MRO algorithm of the old class adopts depth first search
  • The same parent class of the new class only executes the constructor once, while the classic class executes it repeatedly many times.

Of which:

  • As of python2 1. Only old style classes exist. In an old-fashioned class, the class name has nothing to do with type: if x is an old-fashioned class, then X__ class__ The class name of X is defined, but type(x) always returns < type 'instance' >. This reflects that all instances of old-fashioned classes are implemented through a single built-in type called instance, which is different from classes.
  • The new class is in Python 2 2. It is introduced to unify classes and instances. A new class can only be customized by users. If x is an instance of a new class, then type(x) and X__ class__ Is the same result (although this is not guaranteed because the _class _methodof an instance of a new class is allowed to be overridden by the user).
  • Python 2. The default classes in X are classic classes, and only those that explicitly inherit object are new classes
  • Python 3. The default classes in X are all new classes. The classic classes are removed and there is no need to explicitly inherit object s

Inherit search order:

Classic class multi inheritance search order (depth first):

New class multi inheritance search order (breadth first)

class A():
    def __init__(self):
        pass
    def save(self):
        print "This is from A"
class B(A):
    def __init__(self):
        pass
class C(A):
    def __init__(self):
        pass
    def save(self):
        print  "This is from C"
class D(B,C):
    def __init__(self):
        pass
fun =  D()
fun.save()

Classic answer: This is from A
 Answer to the new category: This is from C

2. Q:__ new__ And__ init__ Differences between

__ new__ Responsible for object creation__ init__ Responsible for object initialization.

__ new__: Called when an object is created, an instance of the current object is returned

__ init__: After the object is created, it calls, initializes some instances of the current object, and has no return value.

1. In the class, if__ new__ And__ init__ If it exists at the same time, it will be called first__ new__

class ClsTest(object):
    def __init__(self):
        print("init")
    def __new__(cls,*args, **kwargs):
        print("new")
  
ClsTest()
Output:
new

2. If__ new__ Returns an instance of an object, which is implicitly called__ init__

class ClsTest(object):
    def __init__(self):
        print ("init")
    def __new__(cls,*args, **kwargs):
        print ("new %s"%cls)
        return object.__new__(cls, *args, **kwargs)
  
ClsTest()
Output:
new <class '__main__.ClsTest'>
init

3. __new__ Method returns the constructed object__ init__ No__ init__ No return value.

class ClsTest(object):
     def __init__(cls):
             cls.x = 2
             print ("init")
             return cls
  
ClsTest()
Output:
init
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() should return None, not 'ClsTest'

4. If__ new__ If the instance of the current class cls is not returned correctly, then__ init__ It will not be called, even if it is an instance of the parent class

class ClsTest1(object):
    pass
  
class ClsTest2(ClsTest1):
    def __init__(self):
        print ("init")
    def __new__(cls,*args, **kwargs):
        print ("new %s"%cls)
        return object.__new__(ClsTest1, *args, **kwargs)
  
b=ClsTest2()
print (type(b))
Output:
new <class '__main__.ClsTest2'>
<class '__main__.ClsTest1'>

Reprint: https://www.cnblogs.com/cheyunhua/p/11351698.html

2, Django related issues

1. Q: how to customize ORM

    import pymysql
    '''
    metaclass,The simple explanation is:
    
    After we define a class, we can create an instance based on this class, so: first define a class, and then create an instance.
    
    But what if we want to create classes? Then it must be based on metaclass Create a class, so: define it first metaclass,Then create the class.
    
    The connection is: define first metaclass,You can create classes and finally create instances.
    
    So, metaclass Allows you to create or modify classes. In other words, you can think of classes as metaclass Created "instance".
    
    When we pass in keyword parameters metaclass When the magic works, it indicates Python Interpreter creating Student When,
    
    To pass ModelMetaClass.__new__()Here, we can modify the definition of the class, for example, add a new method, and then return the modified definition.
    '''
    class Field(object):#Define a field class
        def __init__(self,name,column_type):
            self.name = name#Field name
            self.column_type = column_type#Field type
        def __str__(self):
            return "<%s:%s>"%(self.name,self.column_type)
    
    class StringField(Field):#String type Field, inheriting Field
        def __init__(self,name):
            super(StringField,self).__init__(name,"varchar(100)")
            # super(StringField, self).__init__(name,char(20))
    
    class IntegerField(Field):#Numeric type Field, inheriting Field
        def __init__(self,name):
            super(IntegerField,self).__init__(name,"int")
    
    class ModelMetaClass(type):#Define a metaclass
        def __new__(cls, name,bases,attrs):
            '''
            :param name: Name of the class
            :param bases: Class inheritance
            :param attrs:  Properties of class
            :return:
            '''
            if name == "Model":#If the passed in name: class name is Model, no operation will be performed and the relevant parameters of the type class will be returned directly
                return type.__new__(cls,name,bases,attrs)
                # return super(ModelMetaClass, self).__new__(cls,name,bases,attrs)
            print('Found model: %s' % name)  # Print the class name of the current instance
            mapping = dict() #Empty dictionary
            for k,v in attrs.items(): #traversal attributes 
                print('key:%s,value:%s' % (k, v))#Print the key and value of traversing attrs
                if isinstance(v,Field): #Determine whether the Field instance attribute
                    mapping[k] = v #Add to mapping
            # print(mapping)
            for k in mapping.keys(): #Return all keys
                attrs.pop(k) #The content existing in mapping is deleted from the attribute
            attrs["__mapping__"] = mapping  #Set__ mapping__ Property to save the field
            attrs["__table__"] = name #Set the consistency between class name and table name (case insensitive)
            # print(attrs)
            return type.__new__(cls,name,bases,attrs)#Return to the class instance, here is Student
    
    class Model(dict,metaclass = ModelMetaClass):#Create an instance class and set its metaclass
        def __init__(self,**kwargs):
            self.db = pymysql.connect(#Linked database
                host = "localhost",
                user = "root",
                password = "123",
                database = "test"
            )
            self.cursor = self.db.cursor()
            super(Model,self).__init__(**kwargs)
    
        def __getattr__(self, key):
            return self[key]#Returns the property value of the object
            # print(self[key])
        def __setattr__(self, key, value):
            self[key] = value#Set the properties of the object and its corresponding values
    
    
        def save(self):
            fields = [] #An empty list is used to store fields
            args = [] #An empty list is used to store the values of the fields
            for k,v in self.__mapping__.items():
                fields.append(v.name)#At this time, v is an instance of the field subclass, so you can take the class attribute name as the field name
                # print(getattr(self, k, None))
                args.append(getattr(self,k,None))#If self is called at this time, the passed in parameter will be called, and then the value of the variable will be taken as the passed in argument
            sql = "insert into %s(%s) values (%s)"%(
                self.__table__,
                ",".join(fields),
                ",".join([repr(str(i)) for i in args]
                   )) #sql splicing
            self.cursor.execute(sql)
            print(sql)
        def __del__(self):
            '''
            Reclaim memory
            '''
            self.db.commit()
            self.cursor.close()
            self.db.close()
    
    class Student(Model):#Subclass of model instance class
        name = StringField("name")
        # print(name)
        room_id = IntegerField("room_id")
        # print(room_id)
    
    u = Student(name = "Zhang 3",room_id = 6)#The essence is to create a dictionary object
    u.save()#Call the save method of the parent class

2. Q: what third-party library of Django have you used

API development:

  • djangorestframework
  • django-rest-multiple-models
  • django-cors-headers

Query:

  • django-filter
  • django-haystack
  • drf-haystack

Background interface:

  • bootstrap_admin
  • django-jet
  • xadmin
  • django-simpleui
  • django-suit
  • django-grappelli

Commissioning:

  • django-debug-toolbar

Object level permissions:

  • django-guardian

Asynchronous:

  • celery

Rich text editor:

  • django-ckeditor
  • django-tinymce

3. Q: as in Django_ What did you do?

django's class view has the function of automatically finding the specified method through as_ Implementation of view () method

urls.py

from meduo_mall.demo import views

urlpatterns = [
    url(r'register/$', views.Demo.as_view())
]

views.py

from django.views.generic import View


class Demo(View):

    def get(self, request):
        return HttpResponse('get page')

    def post(self, request):
        return HttpResponse('post page')

Why as_view can automatically match the specified method,

Look at the source code first

    @classonlymethod
    def as_view(cls, **initkwargs):  # It's actually a closure that returns the view function
        """
        Main entry point for a request-response process.
        """
        for key in initkwargs:
            if key in cls.http_method_names:
                raise TypeError("You tried to pass in the %s method name as a "
                                "keyword argument to %s(). Don't do that."
                                % (key, cls.__name__))
            if not hasattr(cls, key):
                raise TypeError("%s() received an invalid keyword %r. as_view "
                                "only accepts arguments that are already "
                                "attributes of the class." % (cls.__name__, key))

        def view(request, *args, **kwargs):  # Function: add attributes and call the dispatch method 
            self = cls(**initkwargs)  # Create an instance object of cls, which is the class (Demo) that calls this method
            if hasattr(self, 'get') and not hasattr(self, 'head'):
                self.head = self.get
            self.request = request  # Add request, args and kwargs attributes to the object
            self.args = args
            self.kwargs = kwargs
            return self.dispatch(request, *args, **kwargs)  # Find the specified request method and call it
        view.view_class = cls
        view.view_initkwargs = initkwargs

        # take name and docstring from class
        update_wrapper(view, cls, updated=())

        # and possible attributes set by decorators
        # like csrf_exempt from dispatch
        update_wrapper(view, cls.dispatch, assigned=())
        return view

    def dispatch(self, request, *args, **kwargs):
        # Try to dispatch to the right method; if a method doesn't exist,
        if request.method.lower() in self.http_method_names:  # Determine whether the requested method class view is owned, http_method_names=['get', 'post']
            handler = getattr(self, request.method.lower(), self.http_method_not_allowed)  # If present, remove the method
        else:
            handler = self.http_method_not_allowed
        return handler(request, *args, **kwargs)  # Execute the method

Minimalist Edition

def as_view(): # Check + return view method
    # Some check
    ...
    def view(): # Execution view
        # Add the request, args and kwargs attributes of the object
        ...
        return dispatch() # Call the specified request method
    return view

def dispatch(): # Truly find the specified method and call it
    ...
    return handler()

Call order: as_ view --> view --> dispatch

  • It can be seen that as_view is actually a closure. Its function is to do some verification work and then return the view method

  • The function of the view method is to add three parameters to the request object and call the dispatch method for processing

  • The dispatch method finds the specified request method and executes it

It can be concluded that in fact, the real method to realize the search is the dispatch method

4. Q: what parts does the http request include?

http protocol message

    1. Request message (request line / request header / request data / empty line)

Request line

Find the method field, URL field and HTTP protocol version

For example: get / index html HTTP/1.1

The get method splices the data behind the url, and the transfer parameters are limited

Request method:

                GET,POST,HEAD,PUT,DELETE,OPTIONS,TRACE,CONNECT

Request header (in the form of key value)

User agent: the type of browser that generated the request.

Accept: a list of content types recognized by the client.

Host: host address

Request data

In the post method, the data will be sent to the request in the form of key value

Empty line

Send carriage return and line feed to inform the server that there is no more request header below

    2. Response message (status line, message header, response body)

Status line

Message header

Response body

3, Mysql related issues

1. Q: what are the four isolation levels of transactions?

Concurrency of transactions

1. Dirty reading: transaction A reads the data updated by transaction B, and then B rolls back the operation. Then the data read by A is dirty data

2. Non repeatable reading: transaction A reads the same data multiple times, while transaction B updates and commits the data during the multiple reading of transaction A, resulting in inconsistent results when transaction A reads the same data multiple times.

3. Unreal reading: system administrator A changed the scores of all students in the database from specific scores to ABCDE level, but system administrator B inserted A record of specific scores at this time. When system administrator A finished the change, he found that another record had not been changed, as if there was an illusion. This is called Unreal reading.

Summary: unrepeatable reading and phantom reading are easily confused. Unrepeatable reading focuses on modification, while phantom reading focuses on addition or deletion. To solve the problem of non repeatable reading, you only need to lock the rows that meet the conditions, and to solve the phantom reading, you need to lock the table

MySQL transaction isolation level

Transaction isolation levelDirty readingNon repeatable readingUnreal reading
Read uncommittedyesyesyes
Read committednoyesyes
Repeatable readnonoyes
serializablenonono

 

2. Q: transaction operation?

  1. Start transaction: start transaction;
  2. Rollback: rollback;
  3. Submit: commit;

 

Example:

CREATE TABLE account (
	id INT PRIMARY KEY AUTO_INCREMENT,
	NAME VARCHAR(10),
	balance DOUBLE
);

-- Add data
INSERT INTO account (NAME, balance) VALUES ('zhangsan', 1000), ('lisi', 1000)
SELECT * FROM account;

-- Zhang San transferred 500 yuan to Li Si			
-- 0. Open transaction
START TRANSACTION;

-- 1. Zhang San account -500			
UPDATE account SET balance = balance - 500 WHERE NAME = 'zhangsan';

-- 2. Li Si account +500
-- Error ...
UPDATE account SET balance = balance + 500 WHERE NAME = 'lisi';
			
-- If there is no problem in execution, commit the transaction
COMMIT;
			
-- A problem is found. Roll back the transaction
ROLLBACK;

3. Q: let's talk about the joint index

Indexes on two or more columns are called joint indexes, which are also called composite indexes. For composite index: Mysql uses the fields in the index from left to right. A query can only use part of the index, but only the leftmost part. For example, the index is key index (a,b,c) It can support three combinations of a | a, B | a, B and C, but it does not support B and C When the leftmost field is a constant reference, the index is very effective.

Reprint: https://cloud.tencent.com/developer/article/1030117

4, Linux related issues

1. Q: how do I view memory usage?

top command

You can check the CPU and memory utilization. Of course, these values are the average utilization

Among them,

PID - process identification number
USER - process owner
PR - process priority
NI - process priority value
VIRT - value of virtual memory occupied by the process
RES - value of physical memory occupied by the process
SHR - shared memory value used by the process
S - the state of the process, where s indicates sleep, R indicates running, and Z indicates dead
%CPU - CPU usage rate occupied by the process
%MEM - percentage of physical memory occupied by the process
TIME + - the total CPU TIME occupied after the process is started
Command - the name of the start command that the process started

free command

Check the total memory, usage, idle, etc

ps command to view CPU status

The ps (process status) command is used to report processor status information. Example usage:

ps ux
ps -H -eo user,pid,ppid,tid,time,%cpu,cmd --sort=%cpu
The above commands: the first command is to view the status by default, and the second command specifies the display column and sorting method. Choose any one when using.

2. Q: how to check whether the port is occupied?

1. You can check which ports are opened through "netstat -anp".
(Note: adding the parameter '- n' will turn the application program into port display, that is, the address in digital format, such as NFS - > 2049 and FTP - > 21. Therefore, two terminals can be opened to correspond to the port number corresponding to the program one by one)


2. Then you can view the application program of this PORT through "lsof -i:$PORT" ($PORT refers to the corresponding PORT number). Or you can check the file / etc/services to find the service corresponding to the PORT.
(Note: some ports cannot be found through netstat. The more reliable method is "sudo nmap -sT -O localhost")


3. To close a port, you can:
1) Disable the port through iptables tool, such as:
"sudo iptables -A INPUT -p tcp --dport $PORT -j DROP"
"sudo iptables -A OUTPUT -p tcp --dport $PORT -j DROP"    
2) Or close the corresponding application, and the port will be closed naturally, such as:
"kill -9 PID" (PID: process number)
For example, "netstat -anp | grep ssh"
Display: tcp 0 127.0.0.1:2121 0.0.0.0:* LISTEN 7546/ssh
Then: "kill -9 7546"
Details: https://www.runoob.com/w3cnote/linux-check-port-usage.html


 

Keywords: Python Interview

Added by phpBever on Tue, 01 Feb 2022 20:22:51 +0200