Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of python function parameters, Namespace and function nesting

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "python function parameters, namespaces and function nesting example analysis", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "python function parameters, namespaces and function nesting example analysis" this article.

one。 Function parameters-dynamic transfer parameters 1.1 dynamic position to accept parameters, write a table at the parameter location to show that any content is received

Note:

The relationship between dynamic parameters and position parameters

The relationship between default parameters and dynamic parameters

Summary: order: position parameter, dynamic parameter *, default value parameter

1.2 dynamically receive keyword parameters

Dynamic position parameters can be used in python, but * in this case, only position parameters can be received, not keyword parameters.

You can use * to receive dynamic keyword parameters

Example:

Note: the relationship between keyword parameters and unknown parameters

So the keyword parameter must be followed by the position parameter. Because the arguments are in this order. So the parameters are received in the same order.

That is, the position parameter must precede the keyword parameter. Dynamic receive keyword parameters are also followed by

Final order (*): position parameter > * args > default parameter > * * kwargs

1.3 invincible transfer parameters * args, * * kwargs to be used together

Example:

1.4 another way to transmit parameters for dynamic parameters:

Give a sequence in the argument position, a table, and add a representation to the front of the iterative object to break up the sequence in order.

The table at the position of the parameter combines the received parameters into a tuple

If it is a dictionary, it can also be broken up. But you need to use two *

In the actual reference position: break up

two。 Namespace

We give a name to the space where names and values are stored: namespaces. Our variable stores in this space when it is stored.

Namespace classification:

Global Namespace-> directly in the py namespace file, variables declared outside the function belong to the global namespace

Local namespace-> variables declared in the function are placed in the local namespace

Built-in namespace-> stores the names provided to us by the python interpreter, list, tuple, str, int, which are all built-in namespaces

Loading order:

Built-in namespace

Global namespace

Local namespace (when the function is executed)

Order of values:

Local namespace

Global namespace

Built-in namespace

Scope: scope is scope, which is divided into global scope and local scope according to the effective scope.

Global scope: contains built-in and global namespaces. Can be used anywhere in the entire file (follow line by line from top to bottom).

Local scope: it can be made within the function.

Scope Namespace:

Global scope: global namespace + built-in namespace

Local scope: local namespace

You can use the globals () function to view the contents of the global scope, or you can use locals () to view the variables and function information in the local scope

three。 Nesting of functions

As long as you meet "see" () is the call to the function. If there is no (), it is not a function tuning.

The execution order of the function

four。 Keywords global and nonlocal

Global:

First of all, let's write a code that first calls a variable in the global sound, and then locally calls the variable listen, and changes the value of the variable.

Example 1:

Example 2:

Nonlocal means that a variable in the parent namespace is called in the local scope

These are all the contents of the article "sample Analysis of python function parameters, namespaces, and function nesting". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report