Home >Backend Development >Python Tutorial >What are the Python functions?

What are the Python functions?

PHP中文网
PHP中文网Original
2017-06-20 10:17:091571browse

We can use a piece of code to achieve the function we need, but when we need to reuse this code, copying and pasting is not a cool method. We can use functions to achieve this need

1. Function definition

Function is a logically structured and procedural programming method that encapsulates a string of codes used to complete a specific function through a function name

function Definition:

def Function name (parameter 1, parameter 2....):

''Comments''

Function body

def func1():  #定义函数print('this is a function')  #函数体
func1()  #调用函数
(%)
(%(func1()) 

--> Egon is a good man

None

Advantages of the function: 1. Code reuse

2. Maintain consistency and easy maintenance

3. Good scalability

Note: 1. Functions must be defined first, and then used Variables are similar. An error will be reported if used before definition. 2. The function only detects syntax errors during the definition phase and does not execute the code. Therefore, even if there are undefined variable names in the function body, it will not be executed before the function is called. An error will be reported

3. The return value of the function can be of any type. If multiple values ​​are returned, it must be in the form of a tuple

4. The function of return is to terminate the execution of the function. Return only Executed once, the following content will not be executed.

2. Function parameters

The parameters of a function are divided into formal parameters and actual parameters. When the function is defined, the formal parameters are in parentheses after the function name. , when the function is called, the parameters passed are actual parameters. Formal parameters are only valid inside the function and cannot be referenced externally.

1. Formal parameters

1) Positional parameters: parameters defined in order from left to right def foo(x,y,z)

Positional parameters Values ​​must be passed, and more or less will not work

2) Default parameters: Values ​​have been assigned to formal parameters during the function definition stage. If no values ​​are assigned during the calling stage, there will be a default value def foo(x,y= 10)

When the value changes frequently, it is usually defined as a positional parameter. However, when the value does not change in most cases, it can be defined as a default parameter

Note:

a. Default parameters must be placed after positional parameters

b. Default parameters are usually defined as immutable types

c. Default parameters are only assigned once when they are defined

3) Named keyword parameters: def register(*,name,age) *The formal parameters defined later must be passed by value, and the value must be passed in the form of keywords

2. Actual parameters

1) Positional actual parameters: one-to-one correspondence with positional formal parameters

2) Keyword parameters: When the actual parameters are defined, they are defined in the form of key-values

def foo( x,y)

foo(x=1,y=2)

Keyword parameters do not need to correspond one-to-one with formal parameters like positional parameters, and can break the order restriction

Note: a. When positional parameters and keyword parameters are used together, the positional parameters must precede the keyword parameters

b. It can be either a positional parameter or a keyword parameter. However, a formal parameter can only pass a value once

3) Variable-length parameters:

Variable-length parameters defined by position are represented by *

Defined by keywords Variable type parameters are represented by **

def func(x,y,*args):print(x,y,args)
func(1,2,3,4,5)  --->1 2 (3 4 5)
#When encountering *, it is a positional parameter. Split all the ones after * and match them one by one. The excess is stored in the form of tuples. Together

def func(x,y,**kwargs):print(x,y,kwargs)
func(1,y=2,z=3,a=1,b=2)---->1 2 {'z': 3, 'a': 1, 'b': 2}
#When you encounter **, it is a keyword parameter. Split everything after ** into keywords, match them one by one, and store the excess together in the form of a dictionary

def wrapper(*args,**kwargs): can accept parameters of any form and length

The definition order of parameters: x, y=1,*args,z,* *kwargs, respectively, are positional parameters, default parameters, variable-length positional parameters, named keyword parameters, and variable type parameters

But it should be noted that these parameters will not all appear at the same time

3. Namespace and scope

The namespace stores the binding relationship between names and values, in the form of key-value

Enter the command in the Windows command prompt: import this, You will see this sentence on the last line:

Namespaces are one honking great idea -- let's do more of those!

There are three types of namespaces:

1 ) Built-in namespace: Python comes with it, such as print, int, len.... When the Python interpreter starts, a built-in namespace will be generated

2) Global namespace: defined at the file level The name will be stored in the global name space, which is generated when executing a Python program. To put it simply, it is the variable name without indentation

3) Local name space: the name defined inside the function (or module, class), It only takes effect when a function (module, class) is called, and will be released after the call is completed

The loading sequence is: built-in namespace-->global namespace-->local namespace

The order of values ​​is: local namespace-->global namespace-->built-in namespace

4. Function nesting and scope

1. Function nesting Including nested calls of functions and nested definitions of functions

Nested function calls can be illustrated by the example of finding the maximum value:

def max2(x,y):if x > y:return xelse:return ydef max4(a,b,c,d):
    res1=max2(a,b) #23res2=max2(res1,c) #23res3=max2(res2,d) #31return res3print(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()

2.作用域

1)全局作用域:内置名称空间与全局名称空间的名字属于全局范围,在整个文件的任意位置都能引用

2)局部作用域:属于局部范围,只在函数内部可以被引用,局部有效

一定要注意局部变量和全局变量的作用范围,在局部修改全局变量会出错,在全局范围引用局部变量也会出错

作用域在函数定义时就已经固定了,不会因调用位置而改变

但是如果一定要在局部修改全局变量,也是用办法的,就是在要修改的变量前加一个global

x=1def foo():
    x=10print(x)        

foo()       #10print(x)    #1
x=1def foo():global x
    x=10print(x)

foo()       #10print(x)    #10
def foo():
    x=1def f2():
        x+=xreturn xreturn f2()print(foo())        #会报错UnboundLocalError: local variable 'x' referenced before assignmentdef foo():
    x=1def f2():
        nonlocal x  #告诉Python解释器,这里的x不是局部变量,只会找函数内部的,不会修改全局变量
        x+=xreturn xreturn f2()print(foo())    #会打印出修改后的x的值,2

五、闭包函数

定义在函数内部的函数,该内部函数包含对外部作用域,而非全局作用域的名字的引用,那么该内部函数称为闭包函数

name===func()

闭包函数的特点:a.自带作用域,b.延迟计算(f只是拿到了函数的内存地址,什么时候用,加括号就可以运行)

闭包函数最基本的形式:

def 外部函数名():

  内部函数需要的变量

  def 内部函数名():

    引用外部变量

  return 内部函数名

六、装饰器

1.开放封闭原则:对扩展是开放的,对修改是封闭的

2.装饰器本质是任意可调用的对象,被装饰对象也是任意可调用的对象

3.装饰器的功能是:在不修改被装饰对象源代码及调用方式的前提下,为其添加新的功能

4.装饰器语法:在被装饰对象的正上方的单独一行,写上@装饰器名字

5.有多个装饰器的时候,每行一个,执行时从上往下运行

6.被装饰函数有参数的情况:写成(*args,**kwargs)的形式

装饰器示例一:

#实现缓存网页内容的功能,下载的页面存放于文件中,如果文件内有值(文件大小不为0),# 就优先从文件中读取网页内容,否则,就去下载,然后存到文件中from urllib.request import urlopenimport os

cache_path=r'C:\untitled\0615Python第8天\cache_file.txt'def make_cache(func):def wrapper (*args,**kwargs):if os.path.getsize(cache_path):#有缓存print('\033[45m========>有缓存\033[0m')
            with open(cache_path,'rb') as f:
                res=f.read()else:
            res=func(*args,**kwargs)#下载with open(cache_path,'wb') as f:#制作缓存                f.write(res)return resreturn wrapper


@make_cachedef get(url):return urlopen(url).read()print(get(''))

装饰器示例二:

#为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码db_dic={'egon':'123','alex':'alex3714','yuanhao':'smallb'}


db_path=r'C:\untitled\0615Python第8天\db_dic.txt'with open(db_path,'w',encoding='utf-8') as f:
    f.write(str(db_dic))

login_dic={'user':None,'status':False,
}def auth(func):def wrapper(*args,**kwargs):#加一个验证状态的字典,如果已经登录成功,下次使用就不用重新验证if login_dic['user'] and login_dic['status']:
            res=func(*args,**kwargs)return reselse:
            name=input('name:')
            password=input('password:')
            with open(db_path, 'r', encoding='utf-8') as f:
                auth_dic = eval(f.read())if name in auth_dic and password==auth_dic[name]:print('login ok')
                login_dic['user']=name
                login_dic['status']=True
                res=func(*args,**kwargs)return reselse:print('error')return wrapper

@authdef index():print('welcom to the page')

@authdef home(name):print('welcom  to %s\'s home page'%name)


index()
home('egon')

七、迭代器

1.对于字符串、列表、元组的数据类型,我们可以依据索引来实现迭代的效果,但是字典、集合这种没有索引的数据类型,就需要其他方式

2.Python为了提供一种不依赖索引的迭代方式,为一些对象内置了__iter__方法,obj.__iter__()得到的结果就是迭代器

得到的迭代器既有.__iter__方法,又有.__next__方法

3.迭代器的优点:

  a.提供了一种不依赖索引的取值方式

  b.惰性计算,节省内存

4.迭代器的缺点:

  a.取值不如按照索引取值方便

  b.一次 性的,取值只能往后走,不能往前退

  c.无法获取迭代器的长度

5.for循环实际上会默认调用.__iter__方法

6.判断是否是可迭代对象和迭代器,可以用命令

print(isinstance(str1,Iterable)) --->判断是否为可迭代对象

print(isinstance(str1,Iterator)) --->判断是否为迭代器

八、生成器函数(语句形式和表达式形式)

1.生成器函数:函数体内包含有yield关键字,该函数的执行结果就是生成器

2.生成器实际就是迭代器的一种

3.yield的功能:

  a.与return类似,都可以返回值,但不一样的地方在于yield返回多次值,而return只能返回一次值
  b.为函数封装好了__iter__和__next__方法,把函数的执行结果做成了迭代器
  c.遵循迭代器的取值方式obj.__next__(),触发的函数的执行,函数暂停与再继续的状态都是由yield保存的
4.生成器语句形式应用实例

 1 #模拟linux中tail -f a.txt|grep 'error' |grep '404'的功能 2 import time 3 def tail(filepath,encoding='utf-8'): 4     with open(filepath,encoding='utf-8') as f: 5         f.seek(0,2)  #以末尾为开始位,第0个 6         while True: 7             line=f.readline() 8             if line: 9                 yield line10             else:11                 time.sleep(0.5)12 13 def grep(lines,pattern):14     for line in lines:15         if pattern in line:16             # print(line)17             yield line18 19 g1=tail('a.txt')20 g2=grep(g1,'error')21 g3=grep(g2,'404')22 23 for i in g3:24     print(i)
View Code

5.生成器的表达式形式

def foo():print('starting')while True:
        x=yield #默认就是yield Noneprint('value :',x)

g=foo() 
next(g)  #初始化,等同于g.send(None)
g.send(2)  

将yield赋值给一个变量如x=yield,然后用send()传值,但注意要先做一个类似初始化的操作

g.send(2)的操作实际是先把2传值给yield,再由yield传值给x,send()既有传值的效果,又有next()的效果

生成器表达式形式应用示例

 1 def init(func): 2     def wrapper(*args,**kwargs): 3         g=func(*args,**kwargs) 4         next(g) 5         return g 6     return wrapper 7 @init 8 def eater(name): 9     print('%s ready to eat' %name)10     food_list=[]11     while True:12         food=yield food_list#return None13         food_list.append(food)14         print('%s start to eat %s' %(name,food))15 16 17 e=eater('alex')18 print(e.send('狗屎'))19 print(e.send('猫屎'))20 print(e.send('alex屎'))21 22 23 def make_shit(people,n):24     for i in range(n):25         people.send('shit%s' %i)26 27 e=eater('alex')28 make_shit(e,5)29 #from egon30 #egon老师的例子有味道,但是我又忍不住不用这个
View Code

九、三元表达式

res= x if x>y else y----->判断条件x>y是否为真,为真则把x赋给res,否则把y赋给res

十、列表解析

s='hello'res=[i.upper() for i in s]print(res)          #['H', 'E', 'L', 'L', 'O']

The above is the detailed content of What are the Python functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to build a system?Next article:How to build a system?