The--python concept of common face question finishing

Source: Internet
Author: User
Tags shallow copy unpack python decorator

I hope this article can be updated for a long time and as a Python interview. Each topic is accompanied by detailed answers and a more detailed answer link. This is a concept article, the next one will update the question code chapter.

(a), what do these two parameters mean: *args,**kwargs? Why should we use them?

A: We can use *args (single) If we are not sure how many parameters to pass into a function, or if we want to pass arguments in the form of tuples (tuple) or list. If we do not know how many keyword parameters are passed to the function or if we want to pass in the value of the dictionary as the keyword parameter, we can use **kwargs (double star), args, Kwargs two identifiers are the conventional usage.

Another answer: When the function parameter preceded by an asterisk * indicates that this is a variable positional parameter, two asterisk * * indicates that this is a variable keyword parameter. Asterisk * Unpack the sequence or set (unpack) into positional parameters, two asterisk * * Unpack the dictionary into keyword parameters.

Code-assisted Understanding:

(b), talk about the Python decorator (decorator)

An adorner is essentially a Python function that allows other functions to add extra functionality without any change, and the return value of the adorner is also a function object. It is often used for scenes with demand for facets. For example: Insert log, performance test, transaction processing, cache, permission check, etc. With adorners, we can pull out a lot of similar code that is unrelated to function function to reuse.

For the use of specific adorners look here: Decorators-Liaoche's official website

(iii), a brief description of the Python garbage collection mechanism (garbage collection)

Garbage collection in Python is based on reference counting , which is supplemented by tag-purge and generational collection .

    • Reference count: Python stores a reference count for each object in memory, and if the count becomes 0, the object disappears and the memory allocated to the object is freed.

    • Tag-clear: Some container objects, such as list, Dict, Tuple,instance, and so on, may have a reference loop, and for these loops, the garbage collector periodically recycles the loops (the objects are joined together by reference (pointers) to form a forward graph, and the object forms the node of the graph. and the reference relationship forms the edge of the graph).

    • Generational collection: Python divides memory into three generations based on the object's survival time, and after the object is created, the garbage collector assigns the generation to which it belongs. Each object is assigned a generation, and the younger generation is prioritized, so the later objects are created more easily to be recycled.

If you want to learn more about Python's GC mechanism, click here: [Reprint]python garbage collection mechanism--perfect explanation!

(iv), Python multithreading (multi-threading). Is that a good idea?

Python does not support true multi-threading, and Python provides multithreaded packages. Python has a thing called Global Interpreter Lock (GIL) that ensures that only one thread is executing in your code forever. After the Gil processing, the execution overhead is increased. This means that if you first want to improve the efficiency of code execution, using threading is not a wise choice, of course, if your code is IO-intensive, multithreading can significantly improve efficiency, on the contrary if your code is CPU-intensive in this case multithreading is mostly chicken.

To learn more about multithreading, click here: Detailed multithreaded programming in Python _python

To learn about IO-intensive and CPU-intensive you can click here: Cpu-bound (compute-intensive) and I/O bound (I/O intensive)

(v), explain the Os,sys module is different, and enumerate common module methods?

Official documents:

    • OS templates provide a convenient way to use operating system functions

    • SYS template to access variables used or maintained by the interpreter and functions that interact with the interpreter

Another answer:

The OS module is responsible for the interaction between the program and the operating system, providing access to the underlying operating system interface. The SYS module is responsible for the interaction of the program with the Python interpreter, providing a series of functions and variables for the user to manipulate the environment of the Python runtime.

Some common methods:

Some common usage examples are:

For more detailed use please visit: OS and SYS modules-June drunk

(vi), what is a lambda expression? What good is it?

In a nutshell, lambda expressions are usually used when you need to use a function, but you don't want to use your head to name a function, which is usually called an anonymous function.

The general form of a lambda expression is that a keyword lambda is followed by one or more parameters, immediately following a colon ":", immediately after an expression. A lambda expression is an expression that is not a statement.

To learn more about LAMDBA expressions in Python you can click here: what is the use of LAMBDA expressions? How do I use it? -Python

(vii), what is the function of the pass statement in Python?

The pass statement does nothing, typically as a placeholder or create a placeholder

(eight), how does Python perform type conversion?

Python provides built-in methods for converting variables or values from one type to another.

(ix), Python How to copy an object?

Assignments between objects in Python are passed by reference, if you want to copy an object using copy in the standard template

    • Copy.copy: A shallow copy that copies only the parent object and does not copy the parent object's child objects.

    • Copy.deepcopy: Deep copy, copy parent and child objects.

(10) The difference between __new__ and __init__.

    • __INIT__ is the initialization method, the __new__ method is the real constructor.

    • __new__ is called before the instance is created, and its task is to create and return the instance, which is a static method

    • __init__ is called after the instance is created, and then sets some initial values for the object properties.

Summary: The __new__ method is called before the __init__ method, and the return value of the __new__ method is passed to the __init__ method as the first parameter, and finally __init__ sets some parameters for the instance.

To learn more about both of these methods, click on: __new__ in Python and how to use it

(11), what is the single underline and double underline in python?

    • __NAME__: A convention in which Python's internal name is used to distinguish itself from user-defined names, preventing collisions

    • _name: A convention used to specify the private variables of a variable

    • __name: The interpreter uses _classname__name instead of the name to distinguish it from other classes of the same naming

To learn more about the difference between the two, click: Underline in Python

(12), say a python introspection.

Introspection is a program written by an object-oriented language that can know the type of an object at run time. A simple sentence is the type of object that can be obtained at run time. For example:type (), dir (), GetAttr (), hasattr (), isinstance ()

To fully understand Python introspection, click: Python Introspection (reflection) Guide

Common face question finishing--python concept article

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.