Python core programming learning diary function Programming

Source: Internet
Author: User
/***** Before taking notes, let's first talk about it. I feel that this book is not very suitable for beginners of Python. * Reading this book makes me feel like I have just learned C Language to read <The C Programming Language>. * It may be because my learning ability is not very strong, but it is better to use the syntax of a language in sequence in an entry-level book. * Do not use too many subsequent syntaxes as far as possible. Otherwise, it is easy for learners to get confused. As far as my reading habits are concerned, I am not very happy * Huan saw a sentence in the book: "If you know more about this code, read Chapter N first .". * In addition, I have been too lazy over the past few days. I have nothing to do except play. We planned to see Chapter 1 today, but now we have just finished reading the chapter 13th exception. In addition to the examples in a few books, there is no proper programming practice. Learning a language * is not the same as not using it. * Then, I chatted with my classmates and found that my algorithms were too weak. Good algorithm capabilities are much more important than the languages. Adjust your daily routine. This winter vacation, I can take a look at Python and review the introduction to algorithms... ***/

 

 

Function and functional programming

The process is a simple, special function with no return value. The Python process is a function, because the interpreter implicitly returns the default value None, similar to void in C.

  1. When multiple values are returned, put these values in the container. When multiple values are used, the parallel operators are placed in the tuples, because the syntax definition of the tuples does not necessarily require parentheses.
  2. Keyword parameter: When a function is called, the caller is asked to distinguish the parameter by the parameter name in the function call. In this way, parameters are allowed to be missing or not in order, because the interpreter can match the value of the parameter by providing the keyword.
  3. Execute a function that does not display the defined parameters: Pass the tuples (non-Keyword parameters) or dictionaries (keyword parameters) as parameter groups to the function. Form: func (* tuple_grp_nonkw_args, ** dict_grp_kw_args ). Therefore, the complete Syntax of function calls allowed in python is: func (positional_args, keyword_args, * tuple_nonkw_args, ** dict_grp_kw _ args)
  4. Def statement creation function:
    def function_name(arguments): "function_documentation_string" function_body_suite
  5. Forward reference: similar to other advanced languages, Python cannot reference or call a function before it is declared.Note that as long as the function is not accessed before it is referenced or called, the declared order of the function is unaffected.
  6. Internal/nested functions: create another function in the function body. Python supports static nested fields. Method 1: Define a function within the definition of an external function; Method 2: Use the lamda statement
  7. Function (and method) decorator: it applies additional calls only when a function or method is declared. Syntax: @ decorator (dec_opt_args) \ def funcDecorated (func_opt_args)

    @ G \ @ f \ def foo (): \ pass is equivalent to foo = g (f (foo ))

    The decorator with parameters calls these parameters. Replace your location with your own return value: @ g (arg) \ @ f \ def foo (): \ pass is equivalent to foo = g (arg) (f (foo ))

  8. The decorator is actually a function. They accept function objects for processing. Use decorator: Introduce logs, add timing logic to detect performance, and add transaction capabilities to Functions
  9. In Python, function objects are the same as common objects and can be referenced, accessed, and used as parameters as container elements. The function name is a reference to a function object. Its unique feature is that it can be called.
  10. Format parameters

    The local namespace created when the function is declared creates a name for each parameter value. Once the function is executed, you can access this name

    1. Variable Length parameters:

      (1) variable length parameter (tuples): def function_name ([formal_args,] * vargs_tuple): \ "function_documentation_string" \ function_body_suite

      (2) keyword variable parameter (Dictionary): a parameter is placed in a Dictionary when there are any number of keywords or additional set keywords. The key in the dictionary is the parameter name and the value is the corresponding parameter value. Def function_name ([formal_args,] [* vargst,] ** vargsd): \ function_documentation_string function_body_suite

      Keyword and non-Keyword variable-length parameters may all be in the same function, as long as the keyword dictionary is the last parameter and non-Keyword tuples appear before it.

    2. Python allows the use of lambda keywords to create anonymous functions. Syntax: lambda [arg1 [, arg2,... argN]: The expression parameter is optional. If a parameter is used, the parameter is usually part of the expression.
  11. Variable scope: global variable and local variable.

    Search identifier: search from the local scope. If it is not found in the local scope, the system returns to the full local area. If it is not found, a NameError error is thrown. Python previously does not support multi-layer nesting. Now it can.

    Global Syntax: global var1 [, var2 [,... varN] explicitly references a named global variable in a local field.

  12. Variables defined in external functions that are referenced or used by internal functions are called free variables. If a variable in the external scope (but not in the global scope) is referenced in an internal function, the internal function is regarded as closure.
  13. In terms of syntax, the generator is a function with yield statements. One function or subroutine returns only once, but a generator can pause execution and return an intermediate result-that is, the yield statement function. A value is returned to the caller and the execution is paused. When the next () method of the generator is called, it will accurately continue from the departure location.
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.