Core technology development technology --Python B

Development | Python core technology B

Part B introduces Python custom functions, anonymous function, object-oriented, modular.
Since the foundation of knowledge is not involved, I would be heavy and difficult to explain.

Foreword

目前所有的文章思想格式都是:知识+情感。
知识:对于所有的知识点的描述。力求不含任何的自我感情色彩。
情感:用我自己的方式,解读知识点。力求通俗易懂,完美透析知识。

text

谈谈自定义函数
谈谈匿名函数
谈谈面向对象
谈谈模块化

Talk about custom functions

For, how to customize a function , I think we should need no introduction, and before the Discussion Python function of the article, there is described in detail.

Question: What are the parameters of the function?

A: The positional parameters, default parameters, keyword parameters, non-fixed parameters.

Parameter in Python function can accept any data type, use should be noted that, if necessary, please join in the beginning of the data type checking function .

Problem: Python function name as a parameter can use it?

A: Yes, the name of the direct transfer function, you can parentheses after the function is called.

Question: After the nested function, variable scope of the search order look like?

A: Follow LEGB order, will first look for the local and secondly will find in a nested domain, and then will look at the global, end up looking at the built-in variable, you can not find the error, NameError.

Nested function to ensure data privacy, improve process efficiency.

Problem: the difference nonelocal and global?

A: For a nested function, the internal functions can access external functions defined variables, but not modify, to modify, must be added to this nonlocal keyword.

If you use a global, will be declared directly inside the function variable is a global variable of the same name.

Question: What closure (closure) that?

A: shows a nested closure function, external function returns a function of the internal function name, a function of the internal function of the external reference variables.

Closure function return value and to be called directly brackets. One reason for the use of closures, is make the program more concise and easy to read.

Talk about the anonymous function (lambda)

Question: Why use anonymous function?

A: The use of anonymous functions, allows us to code more concise and easy to read.

Question: What functions are anonymous?

A: lambda is an expression (expression), the body is not a statement (statement) .lambda is a simple expression of only one line, and can not be expanded into a multi-line code blocks.

The so-called expression, is to use a series of "formula" to express something, such as x + 2, x ** 2 and so on;

The so-called statement, it must complete certain functions, such as the assignment complete assignment x = 1, Print statement print (x) the printing is completed, the conditional statement if x <0: complete the selection function and the like.

lambda focus on simple tasks, while the conventional function is responsible for more complex multi-line logic.

Problem: scenarios of lambda?

A: lambda can be combined list generation type used together.

s = [(lambda x: x+1) (x) for x in range(10)]
s
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

lamba can be used with built-in functions, such as reduce (), sort (), filter ().

m = filter(lambda x: x >7, [1, 4, 5, 6, 9, 33, 22])
m
<filter object at 0x000002A0FA605E80>
list(m)
[9, 33, 22]

Question: What is functional programming?

A: The so-called functional programming, refers to a form of each of the code is immutable (the immutable), by the pure function (pure function) of the composition. Pure function here, refers to the function itself mutually independent of each other, for the same input, the output will always be the same, without any side effects.

Functional programming advantages , mainly in its pure function and immutable characteristics make the program more robust, easier to debug (Debug) and testing; major disadvantage that limits more difficult to write.

Question: What is the map ()?

A: map (function, iterable) function, which represents, for each element in the iterable are using the function this function, and returns a new collection can traverse. Combined with the use of lambda.

The map function, for circulation and list comprehension, create a list with the final map () is the fastest. Because the map () function directly written by C language runtime does not require an indirect call by the Python interpreter, and do a lot of internal optimization, so the fastest.

Question: What is the filter ()?

A: filter (function, iterable) functions, which map function and the like, function similarly represents a function object. filter () function expressed iterable each element are determined using the function, and returns True or False, and finally returns True elements of a new set of traversable.

Question: What is reduse ()?

A: reduce (function, iterable) function, which is usually used to make a set number of accumulation operation. function is also a function of the object, which has two predetermined parameters, expressed in iterable each element and the results of the last call, using the calculation function, the final returns a single value.

Object-oriented talk

Introduction: talk about object-oriented, necessarily introduces a process-oriented, process-oriented and is the basic unit of function , is the basic unit of object-oriented classes .

Question: What class is in Python?

A: class, a group with a collection of objects of the same properties and functions.

** issue: the role __ init __ function? **

A: When an object is generated automatically calls the function __ init __ construct the current object's properties. init represents the constructor, do not know if you remember not initialize git repository ~

Question: how to define some constants in a class, each object can easily access these constants without re-configuration?

A: You can write directly attribute class, you can call your own for the class, each instance of the object calls.

Question: If a function does not involve access to modify the properties of this class, and put out like a little inappropriate, how to do it more gracefully?

A: The use of encapsulation class, double underline, expressed as the private property of the class, only internal calls. In fact, the outside can also call, just need to change a format, the class name _ __ attribute name.

Directed to methods, may be used to add decoration to the class of @classmethod , it may also belong to the static function, corresponding to the ordinary function, the decorator plus @staticmethod .

Question: Since the collection class is a group of similar objects, then Can is a collection of a group of similar classes it?

A: Yes, use the inheritance feature classes, subclasses can inherit the parent class. Inheritance using mro table storage.

Note: inheritance search order, the object must have been there, just looking at the object, can not find the class to look for, and then can not find the sort used mro inherit the table order.

Question: inherited super () usage scenario?

A: super can reuse the methods and properties of the parent class is code reuse.

Question: What is the use of abstract class?

A: The abstract class, from abc import ABCMeta, abstractmethod

An abstract class can define interfaces , the successor must implement the requirements of the current interface, otherwise it will error.

Large projects often need to develop a lot of people, after the idea put forward the development team and product groups will first be held product design, PM (Product Manager, Product Manager) write product requirements documents, and iterative; TL (Team Leader, Project Manager ) developers to write a document, the document will be defined in the development of substantially different functions and interfaces of modules, each module how collaboration, unit testing and integration testing, the gradation test line, so a series of logs and monitoring the development process.

Question: What is object-oriented programming?

A: Object-Oriented Programming Software engineering is an important idea. Just as the same dynamic programming algorithm is an important idea, it's not a certain kind of very specific technology, but a manifestation of capacity, is the large-scale projects decoupling and modularization of important ways. In practice, I want to think about, especially abstract thinking to master this skill faster.

Modular talk

Question: Modular can solve any problem?

A: modular, can easily decouple the code, simplify, just call can be realized. The latter configuration changes easier.

Question: introduction module way?

A: You can use: from xx import xx or import xxx

** question: __ init__.py document what is? **

A: In the folder where the file to create a new module the init .py, content can be empty, it can also be used to describe the package module interface of external exposure.

In fact, the Python 2 specification. In Python 3 specification, the init .py and not necessary .

Question: How will the project modular?

A: While running sys.path.append ( ".."), you can change the position of the current Python interpreter, but do not use.

Use as in major projects in the absolute position is the first prerequisite. For a separate project, to pursue all manner of modules, it is best to start from the root directory of the project traceability , which is called the relative absolute path .

The root directory of the project as the basic directory, all the modules call should be to import the root by a layer of way down the index.

Question: What sys.path list representation?

A: Python interpreter in the face of import, it will look for modules in a sys.path list. So, sys.path represents the execution of the program is to find the path to Python import package.

Its first is empty, the first entry is set to absolute address the root project directory.

Problem: how to manage the project package half?

A: The use of Python Virtual Environment (Virtual Runtime Environment). Python can Virtualenv tool, very easy to create a new Python runtime environment.

For each project, it is best to have a separate runtime environment to maintain the purity of packages and modules.

Problem: IF name == ' main ' What does this mean?

A: name as a built-in magic parameter Python, in essence, is an attribute of the module object. When we use the import statement, name will be assigned for the module's name, naturally not equal to the __main__.

Therefore, import when you import the file, it will automatically put all the exposed code execution all over again, and in the IF name '== main internal code' will not be executed.

Note: When import module, the module will perform, so the code is executed last file in the file plus all the code to import. (May be appropriate fantasy about ~)

Conclusion

Python core technology B, primarily a function of knowledge and objects, plus explain the custom functions and anonymous functions, modularity.

I hope you can at the time of writing projects, clever use of modularization, object-oriented thinking -

Guess you like

Origin www.cnblogs.com/Kate-liu/p/11291624.html