Python basic-------functions

Source: Internet
Author: User

Why do I have a function?
Add the code of all functions without distinction

The problem is:
Poor code readability
Code redundancy
Code can be extended to poor

How to solve?
A function is a tool, the process of preparing a tool beforehand is to define a function, and to refer to it is a function call

Conclusion: The function must be used: defined first, then called

Two: Classification of functions

1. Built-in function: built-in
2. Custom functions:
def function name (parameter 1, parameter 2, ...):
"' Notes '
function body

Three: return value of function

Return value: can return any type
No return:none.
Return Value:value
Return VAL1,VAL2,VAL3:(Val1,val2,val3)

Return effect: Only one value can be returned, terminating the execution of the function

Four: use of function parameters

EF Fuck ():
# if 2>1
# Print (2)
# #定义阶段只检查代码的语法不会执行代码, so there's an error inside the function!
#
# def fuck ():
# AAAAAAAAAAAAAAAAAAAAAAAAAAAAA
# #虽然语法没错误但是逻辑有问题, referencing a nonexistent variable name.
# #定义的阶段
# def fuck ():
# print (' Fuck1 ')
# def FUCK1 ():
# print (Fuck ())
# fuck () #调用的阶段
# The Call of the function follows the rule (called) that is defined after it is used!
# def Fuck2 ():
# return #返回值 can return more than one value returned by default as a tuple can only return one value at a time, and will terminate the execution of the function, so put with the last!
#返回类型的值, no return will default to none
# def FUCK3 ():
# return ' A ', ' B ', ' C '
# B=FUCK3 ()
# print (b)
# def fuck3 (x, y): #形参, parameters defined by location! Follow the parameters that must be passed in, one more no, one less!
# Print (' x ', ' y ')
#
# Fuck3 #位置实参, by location-defined arguments! You must first follow the rules of formal parameters! And according to the position and parameter one by one corresponding!
# Fuck3 (x=1,y=2) #关键字实参, corresponding formal parameters according to the keyword! You must first follow the rules of formal parameters! Match names to formal parameters
# Fuck3 (1,y=2) #混合型实参 (both positional arguments and key glyph parameters!) First, you must follow the rules of formal parameters! and follow the positional arguments must precede the keyword arguments

#可变长参数: The number of arguments in the argument is not fixed!
#
#
# def FUCK4 (x,y,*mage): #用 * To handle variable-length position real arguments extra arguments are in the form of tuples and assigned to mage variables! * Take it apart for the position parameter encountered!
# Print (x,y,mage)
# Fuck4 (1,2,3,4,5,6,7,8)
# def FUCK5 (x,y,**kmages): #用 * * to handle variable-length keywords real arguments extra arguments in the form of a dictionary and assign values to the Kmages variable!
# Print (x,y,kmages)
# FUCK5 (x=1,y=2,z=12,ag=123)
Default parameters
The function defines the parameter by default when it is defined
def cout (a=1): #这个a =1 is the default function

#默认参数需要注意的问题一: Must be placed behind the position parameter
#默认参数需要注意的问题二: Default parameters are typically defined as immutable types
#默认参数需要注意的问题三: Default parameters are assigned only once in the definition phase

#命名关键字参数: Parameters defined in the *, such parameters, must be passed the value, and the truth must be in the form of a keyword to pass the value

# def register (*args,name= ' Egon ', age):
# print (args)
# print (name)
# print (age)
#
# Register (name= ' Egon ', age=18)
# Register (1,2,2,3,age=10

V: nested Use of functions

Nested calls to functions
#
# def max2 (x, y):
# if x > y:
# return X
# Else:
# return Y
#
# def max4 (a,b,c,d):
# RES1=MAX2 (A, b) #23
# RES2=MAX2 (res1,c) #23
# RES3=MAX2 (res2,d) #31
# return Res3
#
#
# Print (max4 (11,23,-7,31))

#函数的嵌套定义
Def f1 ():
def f2 ():
Def f3 ():
Print (' from F3 ')
Print (' from F2 ')
F3 ()
Print (' from F1 ')
F2 ()
# Print (F1)
F1 ()

In the function definition phase, the function is defined in the inner layer.

VI: Function Object

#函数是第一类对象: Refers to a function that can be passed as data

def func ():
Print (' from Func ')

#可被引用
# F=func

#可以当做函数的参数
# def func ():
# print (' from Func ')
# def foo (x):
# Print (x)
# x ()
#
# foo (func)

#可以当做函数的返回值
# def foo ():
# print (' from foo ')
# def bar ():
# return Foo
# F=bar ()
# print (f)
# Print (foo)

# x=0
# def F1 ():
# x=1
# def F2 ():
# # X=2
# Print (x)
# return F2
# F=F1 ()
# # print (f)
# f ()

#可以当做容器类型的元素
# DEF select ():
# print (' Select function ')
#
# func_dic={
# ' SELECT ': SELECT,
# }
#
# # Print (func_dic[' select ')
# func_dic[' select '] ()

Seven: Namespaces and scopes

#名字空间: Holds the binding relationship of the name to the value


#名称空间分为三种


#内置名称空间: The Python interpreter comes with its own name, and the Python interpreter starts to generate

#全局名称空间: The name of the file-level definition is stored with the global namespace, which is generated when the Python file executes

# x=1
# def func ():
# Pass
#
# class Foo:
# Pass
#
# import OS
#
# if 1 > 2:
# y=3

#局部名称空间: Defines the name inside the function, the local namespace only takes effect when the function is called, and the function call ends
# def func (x, y): #x =1,y=2
# z=3

# func ()


#三者的加载顺序: Local namespaces, global namespaces, built-in namespaces

#取值: Built-in namespaces, global namespaces, local namespaces


# # MAX=10
# def func (): #x =1
# # MAX=20
# Print (max)
#
#
# func ()

# max=10
#
# def func (): #x =1
# max=20
# # print (max)
# func ()
#
#
# Print (max)
#


# # X=0
# def F1 ():
# # X=1
# def F2 ():
# # X=2
# def f3 ():
# # X=3
# Print (x)
# F3 ()
# F2 ()
#
# F1 ()

#作用域: Scope of action
#全局作用域: The name of the built-in namespace and the global namespace belong to the global scope,
# can be referenced anywhere in the entire file, valid globally
#局部作用域: The name of the local namespace belongs to the local scope,
#只在函数内部可以被引用, partially effective

# x=1
# def foo ():
# def F2 ():
# Print (x)
# F2 ()
# def bar ():
# Print (x)
#
# foo ()
# Bar ()


# def F1 ():
# x=1
# def F2 (): #f2 =value
# # X=2
# Print (x)
# F2 ()
# F1 ()


X=1
def func ():
x=2
Def f1 ():p
# Print (Dir (Globals () [' __builtins__ ']) #全局作用域name
# print (Locals ()) #局部作用域name

Func ()

Print (Globals () is locals ())

#取值查找顺序 Local Scope-----> global scope



Python basic-------functions

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.