Python basic day-6[function, namespace, scope]

Source: Internet
Author: User

return value of the function

Usually there is a parameter function that requires a return value.
Several forms of the return value:
1. Returns none if no return is in the function
2.return returns no restrictions on the type of data, or can return multiple values, in the form of tuples, parentheses can be added without
Return effect:
Terminates the execution of a function and returns only one value at a time.

Use of function parameters
Python is a weakly typed language in which parameters can be passed into any type of data
: You can understand that a parameter is a variable name, and an argument is a variable value. br> def foo (x, y) #x, y is the formal parameter
return x+y
Foo (#1), 2 is argument
positional parameter : Follow from left to right The parameters defined sequentially are called positional parameters.
def foo (x, y): #按位置定义的形参, must be passed the value, one less than one.
Print (x)
Print (y)
Foo (#按位置定义的实参), which corresponds to the formal parameter.
keyword parameter : Refers to the argument as defined, as defined by the Key=value form.
def foo (x, y): #形参还是按位置定义的.
Print (x)
Print (y)
foo (y=1,x=2) #打破位置的限制, do not need to correspond to the parameter one by one, directly specify the value of the pass. But also to follow can not much less.
Note:
1. A positional argument can be mixed with a keyword argument, but the positional argument must precede the keyword argument.
2. A positional argument can be mixed with a keyword argument, but a parameter cannot be passed repeatedly, but only once.
default parameter : Defines the function stage, and the parameter is already assigned a value.
def foo (x, y=10): # parameters are assigned when defined. The
Print (x)
Print (y)
foo (x=2) # argument can not enter the corresponding parameter, and the default argument is not used if it is entered.
Note:
1. Must be placed behind the position parameter.
2. Default parameters are usually defined as immutable types.
3. The default parameter is assigned only once when it is defined.


Application of formal parameters: values are often changed to be defined as positional parameters, and in most cases the default parameters need to be defined.
Application of arguments: How to use it, but follow the corresponding rules.

    variable length parameter : variable length worth is the number of arguments is not fixed * number equal to the position parameter
Variable length arguments defined by location: *
Variable length arguments defined by keyword: * *
def func (X,y,*args): #x =1 y=2, args= (3,4,5,6) Tuple form
Print (x, y)
Print (args)
Func (1,2,3,4,5,6) #实参为位置参数使用 *args
Func (1,2,* (3,4,5,6)) #等同于 func (1,2,3,4,5,6)

D:\Python\Python36-32\python.exe e:/python/day-6/tmp.py1 2(3, 4, 5, 6)1 2(3, 4, 5, 6) Process finished with exit code 0

def func (x, Y, **kwargs): # x=1 y=2, kwargs={' Z ': 6} dictionary Form
Print (x, y)
Print (Kwargs)
Func (x=2, y=4, z=6) #实参为关键字使用 **kwargs

D:\Python\Python36-32\python.exe e:/python/day-6/tmp.py2 4{'z': 6 }process finished with exit code 0

    named keyword parameter : * The formal parameter (*,name,age) defined later must be passed as a keyword
def regist (*,name,age):
Print (name)
Print (age)
Regist (name= ' abc ', AGE=11)

D:\Python\Python36-32\python.exe e:/python/day-6/tmp.pyabconeProcess finished with exit code 0

Formal parameters: Positional parameters, default arguments, *.args named keyword parameters, **kwargs #一般定义形参就按照这种顺序


Nested calls to functions
def max2 (x, y):
If x > Y:
return x
Else
Return y
def mx4 (a,b,c,d):
Res1=max2 (A, B)
RES2=MAX2 (RES1,C)
RES3=MAX2 (RES2,D)
Return Res3
Nested definitions of functions: functions are defined in functions and can only be called at peers
Def f1 ():
def f2 ():
def f3:
Print (' F3 ')
Print (' F2 ')
Print (' F1 ')

Namespaces and Scopes
Namespaces: A binding relationship between a name and a value
Kinds:
  Built-in namespaces
Built-in name inside the Python interpreter, the interpreter generates this space as soon as it starts.
  Global namespaces
The name of the file-level definition is stored in the global namespace, which is generated when the Python file is executed, and the following example
x = 1
def func ():
Pass
Class Foo:
Pass
Import OS
If 1 > 2:
y = 3 #这里y没有顶头写但也是全局变量
  Local name space
Define the name inside the function,
The local namespace only takes effect when the function is called, and execution is invalidated.


Three loading order: built-in =====> global ======> Local
Value: local > Global > Built-in

Example:
max = 10
def tmp ():
max = 20
Print (max)
TMP () #会打印出20 because the local takes precedence over the global


Scope: Scope of action
Kinds:
    Global scope
The name of the built-in namespace and the global namespace, scoped to the global scope. Can be referenced anywhere in the entire file and is globally valid.
    Local scope
The name in the local namespace, scoped to the local scope. Only within the function can be referenced, locally valid

Local scope =====> global scope
The scope of the function is fixed and dead at the time of definition.
Add:
When searching for variables, only the "inside" view is "outside". Can not be reversed. For example, a variable defined inside a function cannot be directly referenced globally.
But if a variable is referenced inside a function, but there is no definition within the function, then the global scope is searched for the variable.
x = 1
def func ():
X =2
Print (Globals ()) #打印全局作用域name
Print (Locals ()) #打印局部作用域name
Func ()


Function object: A function is the first type of object, which refers to a function that can be passed as data
Can be referenced when the function returns a value that can be used when the element of the container

Python basic day-6[function, namespace, scope]

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.