Module 2: function programming (degree course): (Chapter 1st) functions, decorators, iterators, and built-in Methods

Source: Internet
Author: User
Tags variable scope

1. (single answer) when a function is defined, a parameter has a value. We call this parameter ().

Location a parameter B keyword parameter C parameter D default parameterD: If the parameter has a value, we call it the default parameter.Key parameters. The key parameters are real parameters.Under normal circumstances, parameters should be passed to the function in order. If you do not want to use key parameters in order, you only need to specify the parameter name (the parameter with the parameter name specified is the key parameter ),

But remember that the key parameter must be placed after the location parameter (the parameters of the corresponding relationship are determined in order of location ).

2. What is the order of variable search scopes in Python ___?

The answer is legb.

Legb indicates the name search sequence: locals-> enclosing function-> globals-> _ builtins _ locals is the namespace in the function, including local variables and form parameters enclosing external nested functions namespace globals global variables, function definition module namespace builtins built-in module namespace

3. Concept of Recursion

If a function calls itself internally, this function is a recursive function.

Recursive Function data structure is stack-advanced and later

1. There must be a clear termination condition
2. Each time you enter a deeper layer of recursion, the problem scale should be reduced compared to the previous recursion.
3. the recursion efficiency is not high. Too many recursive layers will cause stack overflow (in the computer, function calls are implemented through the stack data structure,
Every time a function is called, a stack frame is added to the stack, and every time the function is returned, the stack will be reduced by a stack frame. Because the stack size is not infinite,
Therefore, stack overflow is caused by too many recursive calls)


Certificate -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

4. Use the list generator to convert the list li = [1, 2, 3, 4, 5, 6, 7, 8, 9] elements larger than 5 Multiply by 2 to form a new list ___.

li = [1, 2, 3, 4, 5, 6, 7, 8, 9]print([item*2 for item in li if item>5])

5. (single answer) based on your understanding of the eval function, select one of the following four options that you think is incorrect ():

The A eval function is used to execute a string expression and return the value of the expression.
The B eval function can convert strings, lists, and metadata.
The C eval function can specify the scope of parameters in the expression.
The D eval function cannot treat dictionary strings as parameters.

Both A and B are the most basic usage of eval functions. The C option examines the eval function parameter understanding. Both globals and locals can specify the scope, and the D option is obviously incorrect.

6. eval function expression parameter scope

Eval (expression [, globals [, locals]) expression -- expression. Globals -- variable scope, global namespace. If it is provided, it must be a dictionary object. Locals -- variable scope, local namespace. If provided, it can be any ing object. Function 1: List, tuple, dict, and string can be converted to each other. Function 2: execute a string expression. Example: A = 10b = 20c = 30g = {'A': 6, 'B': 8} t = {'B': 100, 'C': 10} print (eval ('a + B ', G) This has set the global variable GPRS (eval ('a + B + C', G, t) This sets the global variable g and the local variable L.B, c get from T

This article examines the eval function expression parameter scope,

The first location parameter is the string expression source,
The second location parameter is global scope globals,

The third location parameter is locals in the local scope. Locals takes precedence over globals,

Locals is not specified for the first print, so the values of A and B are all removed from globals, so the result is 14;

For the second print, locals is specified, so the value of B is from locals, so the sum result is 116, so select D.

Certificate ------------------------------------------------------------------------------------------------------------------------------------------

7. Python callable () function

The callable () function is used to check whether an object is callable. If true is returned, the object may still fail to be called. If false is returned, the object called ojbect will never succeed.
For functions, methods, Lambda functions, classes, and class instances that implement the _ call _ method, true is returned.



Module 2: function programming (degree course): (Chapter 1st) functions, decorators, iterators, and built-in Methods

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: 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.