摘要: 有时候,需要接受任意数量的实参,但预先不知道传递给函数的会是什么样的信息。在这种情况下,可将函数编写成能够接受任意数量的键值对--调用语句提供了多少就接受多少。 1、 >>> def a(**x): ## 在形参前面添加双星,可以在实参中添加任意数量的关键字参数,两个*让python创建一个名为x的 阅读全文
posted @ 2021-03-10 22:50 小鲨鱼2018 阅读(314) 评论(0) 推荐(0) 编辑
摘要: 1、如果要让函数接受不同类型的实参,必须在函数定义中将接受任意数量实参的形参放在最后。python先匹配位置参数和关键字参数,再将余下的实参都收集到最后一个形参中(收集参数)。 >>> def a(*x,y): ## 将收集参数放在所有形参的最前面 print(f"- {x}") print(f"! 阅读全文
posted @ 2021-03-10 21:32 小鲨鱼2018 阅读(146) 评论(0) 推荐(0) 编辑
摘要: 有时候, 预先不知道函数需要接受多少个实参,好在python允许函数从调用语句中收集任意数量的实参。 1、 >>> def a(*x): ## 在形参的前面加上*号,表示收集参数,可实现函数接受任意数量的实参 print(x) >>> a("aaa") ('aaa',) >>> a("aaa","b 阅读全文
posted @ 2021-03-10 20:52 小鲨鱼2018 阅读(289) 评论(0) 推荐(0) 编辑
摘要: 1、原始函数 def a(x,y): while x: temp = x.pop() print(f"printing model:{temp}") y.append(temp) def b(x): print("\nThe following model had been printed!") f 阅读全文
posted @ 2021-03-10 17:38 小鲨鱼2018 阅读(246) 评论(0) 推荐(0) 编辑
摘要: 1、不使用函数: list1 = ["aaaa","bbbb","cccc"] list2 = [] while list1: temp = list1.pop() print(f"printing model {temp}") list2.append(temp) print("\nThe fol 阅读全文
posted @ 2021-03-10 16:52 小鲨鱼2018 阅读(233) 评论(0) 推荐(0) 编辑
摘要: 1、 python中向函数传递列表,提高处理列表的效率。 >>> def a(x): ## 定义函数的形参x为可迭代对象 for i in x: print(f"hello,{i.title()}") >>> b = ["aaa","bbb","ccc","ddd"] ## 实参定义为列表,实现向函 阅读全文
posted @ 2021-03-10 15:22 小鲨鱼2018 阅读(472) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(first,meddle,last): b = f"{first} {meddle} {last}" return b.title() >>> a("aaa","bbb","ccc") 'Aaa Bbb Ccc' >>> a("aaa","bbb") ## 少一个实参报错 阅读全文
posted @ 2021-03-10 11:26 小鲨鱼2018 阅读(63) 评论(0) 推荐(0) 编辑
摘要: 1、 >>> def a(x,y): b = {"key1":x,"key2":y} return b >>> a(111,222) {'key1': 111, 'key2': 222} >>> a(111,222,333) ## 多家实参报错 Traceback (most recent call 阅读全文
posted @ 2021-03-10 11:12 小鲨鱼2018 阅读(365) 评论(0) 推荐(0) 编辑
摘要: 1、 def a(first,last): full = f"{first} {last}" return full.title() while True: ## 条件为真的话,一直循环 print("\n\nplease tell me your name:") ## 两个\n,每执行一次循环,空 阅读全文
posted @ 2021-03-10 10:33 小鲨鱼2018 阅读(346) 评论(0) 推荐(0) 编辑