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, Parameter deconstruction and function scope

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

Share

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

This article will explain in detail the example analysis of python function, parameter deconstruction and function scope. I think it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Function definition: def function name (parameter list) function body [return return value] callable () # to determine whether it is the calling object (function name in parentheses, no parentheses are allowed)

Example:

Def add (XBI y):

Return x + y

Add (10Pol 2) # position passes parameters

The keyword "add" is used to pass parameters, corresponding one to one according to the name of the formal parameter.

* * it is required that the location parameter must be passed before the keyword. Shape parameter:

Def add (Xerox 3 and YP4):

Return x + y

Add ()

It is not entered by default, and the default value is xexamples 3 minutes and 4. What has been entered after input shall prevail.

Def sum1 (iterable): # iterable as an iterable object

Result = 0

For x in iterable:

Result + = x

Return result

Def sum1 (iterable): # represents a deformable parameter, which encapsulates multiple parameters into tuples and is immutable. Different from the above: (it is wrong if the parameter is [1 ~ 1 ~ 3])

Result = 0

For x in iterable:

Result + = x

Return result

Variable unknown parameters: the parameter must be transferred by position before the parameter.

As above

Variable keyword parameters: keyword parameters must be used before formal parameters such as kwargs

As follows:

Def showconfig (* * kwargs): # KMurv pairs pass parameters

Print (type (kwargs))

Print (kwargs)

Showconfig (host='192.168.1.1','username'='tom')

Definition order of formal parameters: general parameters, position parameters, keyword parameters

Username,*args,**kwargs

Keyword-only: # A common parameter that appears after an args and becomes keyword-only

Def fn (args,x) # x can only pass parameters by keyword

Def fn3 (xmeme yjinzhongjue z) #''purely to change' z' into keyword-only.

Define the parameter that must use the name as keyword-only

Parameter deconstructing add (* [4Power5]) is equivalent to add (4Magne5) add (* * {'axiajuvle2) add (* {' axiajuva2) add (* {'ajuvv2) add) is equivalent to add (aqinb) add (* {' aqinghuanghuanghuanghezhongbizhu4} .values ())

After the return statement is executed, no other statements within the function will be executed

Function scope:

Variables defined within a function can only be in this function

Def fn ()

Print ('~')

X = 100 # x visible range is only inside the function

Fn ()

Print (x) # cannot output x

Each function opens up a scope

Scope classification:

Global scope

Visible throughout the running environment of the program

Global

Local scope

Visible within functions, classes, etc.

A variable in a local scope becomes a local variable, and the scope of application cannot exceed its scope.

Local scope

In the created .py file, non-functional variables are global variables

X = 1000

Def fn2 ():

Print (2Jing x)

Print (1dint x)

Fn2 ()

Print:

1 1000

2 1000

Function nesting:

Invisible to the outside, visible to the inside

Example 1:

X = 100

Def fn ():

X assignment = 200 # assignment is defined, x becomes local, but it is not assigned, so an error will be reported if you write this

Fn ()

Example 2:

X = 100

Def fn ()

Print (x) # will report an error. Only x is defined as a local variable and there is no assignment at this time.

X = 200 # first defined as local, wait for execution to assign

Print (x)

Fn ()

Global # declares global variables, which are added before the number of lines

Example 3:

X = 100

Def fn ()

Global x # declares x as a global variable

Print (x)

X + = 200

Print (x)

Fn ()

Print (x)

Print:

one hundred

one hundred

three hundred

* closure # in nested functions

Free variables: # variables not defined in the local scope

For example, variables in the scope of the outer function outside the inner layer function are defined.

Closure: the closure is formed when the inner function refers to the free variable of the outer function.

If the inner function does not use the variable of the outer function, the outer variable will die, but if it is used, it will not die.

Def counter ():

C = 0

Def inc ()

Nonlocal c # declares that c is not a local variable of the current function, but is defined at a higher level, and is not a global variable

C + = 1

Return

Nonlocal: not applicable to outermost functions

Scope of the default value

Default value

Def foo (xroom1) # default xroom1

X + = 1

Print (x)

Foo ()

Foo ()

Print:

one

one

Def bar (x = []) # x = [] is the reference type

X + = 1

Print (x)

Foo ()

Foo ()

Print:

[1]

[1] [1]

Bar.defaults

([1Jing 1])

.defaults # View parameter default properties

If there is a keyword-only:

.kwdefults

Example:

Def foo (xyz,m=123,*,n='abc',t = [1jue 2]:

Print (foo.defaults,foo.kwdefults)

(123,) {'nforth Vol. ABCLERIMETRY]: [1Phone2]}

A + = [5] # is equivalent to a.extend ([5]). Modify it locally and will not change the address value.

A = a + [5] # is equivalent to creating a new list

* variable name resolution principle LEGB

Local

Enclosing

Global

Namespace # print (open) print open of the Build-in built-in module

So the search order of a noun is LEGB.

The demise of a function

In essence, the reference count is zero.

This is the end of this article on "sample analysis of python functions, parameter deconstruction and function scope". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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