对python3 中方法各种参数和返回值详解


Posted in Python onDecember 15, 2018

如下所示:

# -*- coding:utf-8 -*-
# Author: Evan Mi
 
# 函数
 
 
def func1():
  print('in the func1')
  return 0
 
# 过程
 
 
def func2():
  print('in the func2')
 
"""
多个值用逗号分割后返回,会分装到一个tuple中返回,
接收的时候,如果使用一个变量接收,那么这个接收变量就是一个tuple类型的
如果接收的时候也用逗号分割多个值来接收,那么可以分别对应返回tuple中的每一个值
"""
 
 
def func3():
  return 1, 'hello', ['alex', 'wupei'], {'name': 'alex'}
 
x = func1()
y = func2()
z = func3() # 一个值接收,是一个tuple
q, w, e, r = func3() # 用对应个数的值接收,每个变量对应tuple对应位置的值
print(x)
print(y)
print(z)
 
print("center".center(100, '*'))
print(q)
print(w)
print(e)
print(r)
 
print("center".center(100, '*'))
 
# 定义一个方法
 
 
def test(x_arg, y_arg):
  print(x_arg)
  print(y_arg)
 
 
test(1, 2) # 位置参数调用
test(y_arg=3, x_arg=5) # 关键字参数调用,直接给形式参数赋值
 
 
def test1(x_arg, y_arg, z_arg):
  print(x_arg)
  print(y_arg)
  print(z_arg)
 
 
# 关键字参数不能写到位置参数之前
test1(1, z_arg=2, y_arg=3)
 
 
# 默认值参数
def test2(x_arg, y_arg=2):
  print(x_arg)
  print(y_arg)
 
 
print("center".center(100, '*'))
test2(1)
print("center".center(100, '*'))
test2(1, 3)
print("center".center(100, '*'))
test2(y_arg=5, x_arg=8)
 
"""
在*args 前面有参数(x, *args),那么(1,2,3,4,5)正确,(x=1,2,3,4,5)正确,(2,3,4,5,x=1)错误,给x多次赋值了
在*args 后面有参数(*args,x,y)那么x,y只能采用关键字赋值方式(1,2,3,4,5,x=6,y=8) 
"""
 
 
def test3(*args):
  print(args)
 
 
test3(1, 2, 3, 4, 5)
test3(*[1, 2, 3, 4, 5])
print("center".center(100, '*'))
 
 
def test4(x_arg, *args):
  print(x_arg)
  print(args)
 
 
test4(1, 2, 3, 4, 5)
 
 
def test5(**kwargs):
  print(kwargs)
  print(kwargs['name'])
 
 
test5(name='alex', age=8)
test5(**{'name': 'Evan', 'age': 8})
 
 
def test6(*args, xx):
  print(args)
  print(xx)
 
# **kwargs 必须在最后
#  def test7(**kwargs,xx=3): 这样定义是错误的
 
 
print("test6")
# test6(1, 2, 3, 4, 5999, xx=4)
"""
*args 接收位置参数,转换为tuple
**kwargs 接收关键字参数,转换为dict
位置参数不能写在关键字参数的后面
"""
 
 
def tt(xx=1, *args, **kwargs):
  print(xx)
  print(args)
  print(kwargs)
 
 
def ttt(xx, **kwargs):
  print(xx)
  print(kwargs)
 
 
def tttt(*args, xx, **kwargs):
  print(args)
  print(xx)
  print(kwargs)
 
# kwargs接收的关键字参数的名字不能和函数列表中已有的其他参数相同
# tt(2, 3, 4, 5, name=100, age=199, xx=98)
# 出现了xx,优先赋值给参数列表中的xx,而不是在dict中加入关键字为xx的key-value对
# ttt(x=99, y=99, xx=43)
 
tttt(1,2,3,4,5,x=100,y=33,xx=8)

以上这篇对python3 中方法各种参数和返回值详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python jieba分词并统计词频后输出结果到Excel和txt文档方法
Feb 11 Python
tensorflow 获取变量&打印权值的实例讲解
Jun 14 Python
利用Python实现原创工具的Logo与Help
Dec 03 Python
Python实现的服务器示例小结【单进程、多进程、多线程、非阻塞式】
May 23 Python
Python3日期与时间戳转换的几种方法详解
Jun 04 Python
Django MEDIA的配置及用法详解
Jul 25 Python
Python 通过微信控制实现app定位发送到个人服务器再转发微信服务器接收位置信息
Aug 05 Python
Python调用scp向服务器上传文件示例
Dec 22 Python
在python3中使用shuffle函数要注意的地方
Feb 28 Python
Python如何使用paramiko模块连接linux
Mar 18 Python
opencv+python实现鼠标点击图像,输出该点的RGB和HSV值
Jun 02 Python
Python模块常用四种安装方式
Oct 20 Python
对python中的argv和argc使用详解
Dec 15 #Python
Python输出\u编码将其转换成中文的实例
Dec 15 #Python
对python:print打印时加u的含义详解
Dec 15 #Python
Python 最大概率法进行汉语切分的方法
Dec 14 #Python
python实现任意位置文件分割的实例
Dec 14 #Python
pytorch permute维度转换方法
Dec 14 #Python
Python语言快速上手学习方法
Dec 14 #Python
You might like
php实现socket推送技术的示例
2017/12/20 PHP
php web环境和命令行环境下查找php.ini的位置
2019/07/17 PHP
jQuery插件EnPlaceholder实现输入框提示文字
2015/06/05 Javascript
Javascript实现的Map集合工具类完整实例
2015/07/31 Javascript
jquery自定义插件开发之window的实现过程
2016/05/06 Javascript
原生JavaScript编写canvas版的连连看游戏
2016/05/29 Javascript
基于jquery实现弹幕效果
2016/09/29 Javascript
jquery仿苹果的时间/日期选择效果
2017/03/08 Javascript
JavaScript实现QQ聊天消息展示和评论提交功能
2017/05/22 Javascript
浅谈Angular2 ng-content 指令在组件中嵌入内容
2017/08/18 Javascript
JS加密插件CryptoJS实现的DES加密示例
2018/08/16 Javascript
vue2.0 路由模式mode="history"的作用
2018/10/18 Javascript
详解关闭令人抓狂的ESlint 语法检测配置方法
2019/10/28 Javascript
javascript实现时钟动画
2020/12/03 Javascript
js实现随机点名
2021/01/19 Javascript
vc6编写python扩展的方法分享
2014/01/17 Python
Python中文件遍历的两种方法
2014/06/16 Python
使用python远程操作linux过程解析
2019/12/04 Python
python时间与Unix时间戳相互转换方法详解
2020/02/13 Python
python实现猜单词游戏
2020/05/22 Python
python filecmp.dircmp实现递归比对两个目录的方法
2020/05/22 Python
浅谈django框架集成swagger以及自定义参数问题
2020/07/07 Python
python实现三壶谜题的示例详解
2020/11/02 Python
python time()的实例用法
2020/11/03 Python
Pytest测试框架基本使用方法详解
2020/11/25 Python
《罗布泊,消逝的仙湖》教学反思
2014/03/01 职场文书
销售队伍口号
2014/06/11 职场文书
2014年个人债务授权委托书范本
2014/09/22 职场文书
普通党员个人整改措施
2014/10/27 职场文书
司法局2014法制宣传日活动总结
2014/11/01 职场文书
2014年行政后勤工作总结
2014/12/06 职场文书
安全主题班会教案
2015/08/12 职场文书
小学班主任工作随笔
2015/08/15 职场文书
写给汽车4S店的创业计划书,拿来即用!
2019/08/09 职场文书
导游词之广州陈家祠
2019/10/21 职场文书
pytorch 梯度NAN异常值的解决方案
2021/06/05 Python