python函数声明和调用定义及原理详解


Posted in Python onDecember 02, 2019

这篇文章主要介绍了python函数声明和调用定义及原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

函数是指代码片段,可以重复调用,比如我们前面文章接触到的type()/len()等等都是函数,这些函数是python的内置函数,python底层封装后用于实现某些功能。

一.函数的定义

在Python中,定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回;如果没有return语句,默认返回None:

def functionname( parameters ):
  "函数说明"
  function_suite
  return [expression]

例如:写一个函数输出'hello world'

def cusom_print():
  print("hello world")

二.函数的调用

当在py文件中,代码一行一行执行,如果遇到函数的定义,编译器会自动跳过,执行函数之后的代码,如果想调用函数直接调用即可。

注意:函数在调用之前必须先声明。python中的内置函数如:print/type函数等等已经在python编译器内部声明并且定义好了,我们只管调用即可,不需要关心具体内部如何实现。示例代码如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): shuopython.com
@WeChat Official Account(微信公众号):猿说python
@Github:www.github.com
 
@File:python_function.py
@Time:2019/10/3 10:48
 
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
"""
def custom_print():
  print("hello world")
  print("hello world")
  print("hello world")
custom_print()

输出结果:

hello world
hello world
hello world

代码分析:代码执行到第15行时,编译器发现这是一个函数声明,编译器并不会执行,会自动跳到函数末尾第20行,编译器发现20行是在调用custom_print()函数,会直接进入custom_print()函数执行函数内的代码第16/17/18行直到函数结束,这就是整个运行过程。

三.函数传参

函数可以通过外部传递参数,比如:print()函数,可以直接传递字符串并打印字符串;也可以不传递参数,比如上面的custom_print函数,根据自己的需求而定.

函数声明的时候定义的参数叫做形参;外部调用函数传递的参数叫做实参;函数的参数有两者类型:

1.常规参数
常规而言,函数默认有几个形参,在外部调用时就需要传递多少个实参,示例代码如下:

def cusom_print1(x):
  print("cusom_print1 : x={}".format(x))
 
def cusom_print2(x,y):
  print("cusom_print2 : x={}".format(x))
  print("cusom_print2 : y={}".format(y))
 
def cusom_print3(x,y,z):
  print("cusom_print3 : x={}".format(x))
  print("cusom_print3 : y={}".format(y))
  print("cusom_print3 : z={}".format(z))
 
cusom_print1(1)
cusom_print2(1,2)
cusom_print3(1,2,3)

输出结果:

cusom_print1 : x=1
cusom_print2 : x=1
cusom_print2 : y=2
cusom_print3 : x=1
cusom_print3 : y=2
cusom_print3 : z=3

2.缺省参数

在函数参数中,除了常规参数还有缺省参数,即缺省参数有一个默认值,如果外部调用该函数没有给缺省参数传递参数,该形参直接取默认参数值;如果外部调用时给缺省参数传递了参数,那么该形参的值应该等于外部传递的参数,带有缺省参数的函数也被称为缺省函数,示例代码如下:

def cusom_print4(x,y=2,z=3): # x=2,z=3 缺省参数
  print("cusom_print4 : x={}".format(x))
  print("cusom_print4 : y={}".format(y))
  print("cusom_print4 : z={}".format(z))
  print("***"*20)
 
cusom_print4(1)
cusom_print4(1,4)
cusom_print4(1,4,3)

输出结果:

cusom_print4 : x=1
cusom_print4 : y=2
cusom_print4 : z=3
************************************************************
cusom_print4 : x=1
cusom_print4 : y=4
cusom_print4 : z=3
************************************************************
cusom_print4 : x=1
cusom_print4 : y=4
cusom_print4 : z=3
************************************************************

注意:

1.缺省参数都有一个默认值,如果外部没有给缺省参数传递参数,那么直接取默认值;否则等于外部传递的参数值

2.缺省参数必须写在函数形参的末尾

# 错误写法
def cusom_print4(x,y=2,z):
print("cusom_print4 : x={}".format(x))

3.不定长参数

除了上面两者,在函数的参数中还有一种不定长参数,即:函数的形参长度/类型都不固定

四.函数返回值return

函数的返回值可有可无,根据自己的使用需求而定。如果函数没有return返回值,默认会返回None,即空值。和 False 不同,它不表示 0,也不表示空字符串,而表示没有值,也就是空值。

五.重点总结

1.函数的声明必须在调用之前,否则会报错.

2.注意缺省参数的参数写法

3.函数没有使用return,默认返回None

这篇文章主要介绍了python函数声明和调用定义及原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

Python 相关文章推荐
基于循环神经网络(RNN)实现影评情感分类
Mar 26 Python
python利用requests库进行接口测试的方法详解
Jul 06 Python
解决tensorflow1.x版本加载saver.restore目录报错的问题
Jul 26 Python
Python爬虫框架scrapy实现downloader_middleware设置proxy代理功能示例
Aug 04 Python
python中的不可变数据类型与可变数据类型详解
Sep 16 Python
Python并行分布式框架Celery详解
Oct 15 Python
Python列表list排列组合操作示例
Dec 18 Python
python监测当前联网状态并连接的实例
Dec 18 Python
python3反转字符串的3种方法(小结)
Nov 07 Python
pygame实现弹球游戏
Apr 14 Python
python中执行smtplib失败的处理方法
Jul 01 Python
Python用K-means聚类算法进行客户分群的实现
Aug 23 Python
python return逻辑判断表达式实现解析
Dec 02 #Python
Python线程障碍对象Barrier原理详解
Dec 02 #Python
python 循环数据赋值实例
Dec 02 #Python
python创建n行m列数组示例
Dec 02 #Python
python 创建一维的0向量实例
Dec 02 #Python
python 初始化一个定长的数组实例
Dec 02 #Python
Python生态圈图像格式转换问题(推荐)
Dec 02 #Python
You might like
PHP循环语句笔记(foreach,list)
2011/11/29 PHP
PHP MYSQL实现登陆和模糊查询两大功能
2016/02/05 PHP
编辑浪子版表单验证类
2007/05/12 Javascript
很棒的学习jQuery的12个网站推荐
2011/04/28 Javascript
js防止表单重复提交实现代码
2012/09/05 Javascript
javascript真的不难-回顾一下基础知识
2013/01/15 Javascript
利用函数的惰性载入提高javascript代码执行效率
2014/05/05 Javascript
AngularJS中的Directive自定义一个表格
2016/01/25 Javascript
JavaScript编写检测用户所使用的浏览器的代码示例
2016/05/05 Javascript
JS中script标签defer和async属性的区别详解
2016/08/12 Javascript
详解vue组件基础
2018/05/04 Javascript
Vue 样式绑定的实现方法
2019/01/15 Javascript
layer.open组件获取弹出层页面变量、函数的实例
2019/09/25 Javascript
JavaScript中变量提升机制示例详解
2019/12/27 Javascript
关于Node.js中频繁修改代码重启服务器的问题
2020/10/15 Javascript
再也不怕 JavaScript 报错了,怎么看怎么处理都在这儿
2020/12/09 Javascript
Python多进程同步Lock、Semaphore、Event实例
2014/11/21 Python
深入学习python的yield和generator
2016/03/10 Python
python利用urllib和urllib2访问http的GET/POST详解
2017/09/27 Python
Python设计模式之状态模式原理与用法详解
2019/01/15 Python
python中logging模块的一些简单用法的使用
2019/02/22 Python
python开发之anaconda以及win7下安装gensim的方法
2019/07/05 Python
python socket通信编程实现文件上传代码实例
2019/12/14 Python
Python3 实现减少可调用对象的参数个数
2019/12/20 Python
Pytorch训练过程出现nan的解决方式
2020/01/02 Python
flask利用flask-wtf验证上传的文件的方法
2020/01/17 Python
Python3创建Django项目的几种方法(3种)
2020/06/03 Python
Django-simple-captcha验证码包使用方法详解
2020/11/28 Python
python 指定源路径来解决import问题的操作
2021/03/04 Python
美国顶级防滑鞋:Shoes For Crews
2017/03/27 全球购物
美国高级音响品牌:Master&Dynamic
2018/07/05 全球购物
你经历的项目中的SCM配置项主要有哪些?什么是配置项?
2013/11/04 面试题
留学自荐信
2013/10/10 职场文书
公司请假条范文
2014/04/11 职场文书
Nginx同一个域名配置多个项目的实现方法
2021/03/31 Servers
mysql连接查询中and与where的区别浅析
2021/07/01 MySQL