Python engineers need to have face questions

1. What are the characteristics and advantages of Python have?
A: As an introductory programming language, Python has the following characteristics and advantages:

  • Interpretable
  • Dynamic nature
  • Object-Oriented
  • Concise simple
  • Open source
  • It has strong community support

2. What is the difference between deep and shallow copy copy that?
A: deep copy is to copy one object to another object, which means that if you make a copy of an object change will not affect the original object. In Python, we use the function DeepCopy () perform a deep copy, Copy import module, as follows:

>>> import copy
>>> b=copy.deepcopy(a)

And shallow copy copy sucked an object reference to another object, so if we change the copy, will affect the original object. We use the function function () perform a shallow copy, use as follows:

>>> b=copy.copy(a)

3. The difference between lists and tuples are?
A: The main difference between the two is that lists are mutable, and tuples are immutable. For example, as shown below:

>>> mylist=[1,3,3]
>>> mylist[1]=2
>>> mytuple=(1,3,3)
>>> mytuple[1]=2
Traceback (most recent call last):
File "<pyshell#97>", line 1, in <module>
mytuple[1]=2

You receive the following error:

TypeError:tupleobject does not support item assignment

4, how to implement multiple threads in Python?
A: A thread is a lightweight process, multi-threaded execution allows us to once multiple threads.
As we all know, Python is multi-threaded language, its built-in multithreaded tool kit.
In Python GIL (Global Interpreter Lock) to ensure the implementation of a single thread.
GIL save a thread and pass it to perform some action before the next thread, which will enable us to produce the illusion of running in parallel.
But in fact, just the thread turns run on CPU, of course, all the memory transfer will increase the pressure of program execution.

5. Explain inheritance in Python
A: When a class inherits from another class, it is called a subclass / derived class, inherited from the parent class / base class / superclass. It inherits / Get all class members (attributes and methods).
Inheritance allows us to re-use code, but also easier to create and maintain applications. Python supports the following types of inheritance:

  • Single inheritance: single class inherits from a base class
  • Multiple inheritance: a plurality of class inherits from the base class
  • Multi-inheritance: single class inherits from a base class, which inherits from the base class to another
  • Hierarchical inheritance: a plurality of single class inherits from the base class
  • Mixed Inheritance: Inheritance of two or more types of mixed

6. What is the monkey patches?
Dynamically modify a class or module during operation.

>>> class A:
    def func(self):
            print("Hi")
>>> def monkey(self):
        print "Hi, monkey"

>>> m.A.func = monkey
>>> a = m.A()
>>> a.func()

Operating results as follows:

Hi, Monkey

7, please use the * args and explain the meaning of kwargs **
in python, * args and ** kwargs commonly used in the function definition.
* args and kwargs function allows you to pass a variable number of parameters,
even though the function definition do not know when the caller will pass several parameters.
ps: * args and
kwargs just a habit we all abide by, any name can be written.

  • Examples 1. * args
    * args can receive varying amounts of non-keyword parameters, will be converted to a position parameter tuple (parameter set of non-value pairs),
    the example as shown in the following codes:
def func(*args):
    for i in args:
        print(i)
func(1,2,3,4)

operation result:

1
2
3
4
  • 2. ** kwargs example
    kwargs allows you to pass parameters without quantitative keywords.
    If you do not need to define a quantitative parameter named function, you should use
    kwargs, and
    it will be converted to a keyword parameter dict (key-value pair parameter set), as shown in the example code below:
def func(**kwargs):
    for i in kwargs:
        print(i,kwargs[i])
func(a=1,b=2,c=3,d=4)

operation result:

a 1
b 2
c 3
d 4

8. What is the difference between the new and python init is?
New__ is a static method, and __init__ is an instance method.
New__ method returns an instance is created, and __init__ returns nothing.
Only _ __init__ can be called back when _new__ returns an instance of cls.
call __new when you create a new instance
initialized with an instance when __init
.

9, explain the ternary operator in Python
, unlike C ++, we do not have in Python:?, But we have this:
[on to true] IF [expression The] the else [on false]
If the expression is True, the implementation [on true] statements. Otherwise, execution [on false] statements.
Here is its methods:

>>> a,b=2,3
>>> min=a if a<b else b
>>> min

operation result:

2
>>> print("Hi") if a<b else print("Bye")

operation result:

Hi

10, in Python is how to manage memory?
Python has a private heap space to hold all the objects and data structures. As developers, we can not access it, it is explained in management. But with the core API, we can access some tools. Python memory manager controls memory allocation.
In addition, the built-in garbage collector reclaims all unused memory, so it is suitable for heap space.

Published 180 original articles · won praise 13 · views 7153

Guess you like

Origin blog.csdn.net/weixin_45794138/article/details/104911689