Python函数第五节
来源:互联网 发布:因为你是范晓萱 知乎 编辑:程序博客网 时间:2024/06/10 00:17
2.定义一个func(name,callback=None),效果如下。assert func("lilei") == "Lilei"assert func("LILEI",callback=string.lower) == "lilei"assert func("lilei",callback=string.upper) == "LILEI""""import stringdef func2(name, callback=None): if not isinstance(name, str): return 'not a name' if callback == None: return name.capitalize() return callback(name)assert func2("lilei") == "Lilei"assert func2("LILEI", callback=string.lower) == "lilei"assert func2("lilei", callback=string.upper) == "LILEI"# 对于string中的函数string.lower和string.upper是一个函数,通过回调函数callback,通过函数参数传递到其他代码执行的<pre name="code" class="python">4.定义一个func(*kargs),该函数效果如下返回最短的字符串。assert func(222,1111,'xixi','hahahah') == "xixi"assert func(7,'name','dasere') == 'name'assert func(1,2,3,4) == None"""def func4(*kargs): list_filter = filter(lambda k: isinstance(k, str), kargs) if not list_filter: return None len_list = [len(i) for i in list_filter] min_len = min(len_list) return list_filter[len_list.index(min_len)]#关于index的用法返回索引 str.index(self, begin, end)
0 0
- Python函数第五节
- 函数第五节
- Python公开课第五节
- 第五节 编写变长参数函数
- js第五节-函数参数的传递
- 第五节 字符串处理与字符串函数
- 第五节 函数与闭包
- 第五章 第五节:信息函数&&聚合函数&&加密函数
- python爬虫第五节课及作业答案
- PHP学习第五节----函数的高级运用
- 第五节 实现接口
- 第五节 电能质量
- 第一章 --- 第五节
- 第二章 --- 第五节
- 第三章 --- 第五节
- 第五节 定时器使用
- php学习 第五节
- java笔记第五节
- Log4.properties配置详解
- [线段树] Chef and Array
- ActionResult 返回类型
- PAT A1002 A+B for Polynomials (25)
- ios 文件上传
- Python函数第五节
- MVC各层的典型实现
- iOS基础之属性修饰符的区别
- springMVC日期转化注释
- 仿新浪TabLayout代码片段
- Android Studio 的 Gradle大白话讲解
- maven连同所有依赖包一起打包成jar包
- 杭电oj DNA Sorting
- 高级加密标准AES的工作模式(ECB、CBC、CFB、OFB)