python存储函数 python保存函数

python 字典可以储存函数吗

Python中是没有switch的, 所以有时我们需要用switch的用法, 就只能通过if else来实现了. 但if else写起来比较冗长,

超过十余年行业经验,技术领先,服务至上的经营模式,全靠网络和口碑获得客户,为自己降低成本,也就是为客户降低成本。到目前业务范围包括了:成都网站设计、成都做网站,成都网站推广,成都网站优化,整体网络托管,小程序制作,微信开发,App定制开发,同时也可以让客户的网站和网络营销和我们一样获得订单和生意!

这时就可以使用Python中的dict来实现, 比switch还要简洁. 用法如下:

如果是key1的情况就执行func1, 如果是key2的情况就执行func2...(func1, func2...所有的函数的参数形式需要相同),

假设各个函数参数均为(arg1, arg2):

dictName = {"key1":func1, "key2":func2, "key3":func3"...}#字典的值直接是函数的名字,不能加引号dictName[key](arg1, arg2)

示例代码如下:

#!/usr/bin/python#File: switchDict.py#Author: lxw#Time: 2014/10/05import redef add(x, y): return x + ydef sub(x, y): return x - ydef mul(x, y): return x * ydef div(x, y): return x / ydef main():

inStr = raw_input("Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.\n")

inList = re.split("(\W+)", inStr)

inList[1] = inList[1].strip() print("-------------------------") print(inList) print("-------------------------") #Method 1:

if inList[1] == "+": print(add(int(inList[0]), int(inList[2]))) elif inList[1] == "-": print(sub(int(inList[0]), int(inList[2]))) elif inList[1] == "*": print(mul(int(inList[0]), int(inList[2]))) elif inList[1] == "/": print(div(int(inList[0]), int(inList[2]))) else: pass

#Method 2:

try:

operator = {"+":add, "-":sub, "*":mul, "/":div} print(operator[inList[1]](int(inList[0]), int(inList[2]))) except KeyError: passif __name__ == '__main__':

main()

Output:

PS J:\ python .\switchDict.py

Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.1 + 2

-------------------------['1', '+', '2']-------------------------

3

3PS J:\ python .\switchDict.py

Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.4 - 9

-------------------------['4', '-', '9']-------------------------

-5

-5PS J:\ python .\switchDict.py

Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.6 / 5

-------------------------['6', '/', '5']-------------------------

1

1PS J:\ python .\switchDict.py

Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.1 9 9

-------------------------['1', '', '9', ' ', '9']-------------------------PS J:\ python .\switchDict.py

Please input the easy expression:(e.g. 1 + 2.But 1 + 2 + 3 are not accepted.1 ( 9

-------------------------['1', '(', '9']-------------------------PS J:\

个人感觉, 如果想用switch来解决某个问题, 并且每种情况下的操作在形式上是相同的(如都执行某个函数并且这些函数有

相同的参数), 就可以用这种方法来实现.

python中函数包括

1. print()函数:打印字符串

2. raw_input()函数:从用户键盘捕获字符

3. len()函数:计算字符长度

4. format(12.3654,'6.2f'/'0.3%')函数:实现格式化输出

5. type()函数:查询对象的类型

6. int()函数、float()函数、str()函数等:类型的转化函数

7. id()函数:获取对象的内存地址

8. help()函数:Python的帮助函数

9. s.islower()函数:判断字符小写

10. s.sppace()函数:判断是否为空格

11. str.replace()函数:替换字符

12. import()函数:引进库

13. math.sin()函数:sin()函数

14. math.pow()函数:计算次方函数

15. 3**4: 3的4次方

16. pow(3,4)函数:3的4次方

17. os.getcwd()函数:获取当前工作目录

18. listdir()函数:显示当前目录下的文件

19. socket.gethostbyname()函数:获得某主机的IP地址

20. urllib.urlopen(url).read():打开网络内容并存储

21. open().write()函数:写入文件

22. webbrowser.open_new_tab()函数:新建标签并使用浏览器打开指定的网页

23. def function_name(parameters):自定义函数

24. time.sleep()函数:停止一段时间

25. random.randint()函数:产生随机数

python有多少内置函数

Python内置函数有很多,为大家推荐5个神仙级的内置函数:

(1)Lambda函数

用于创建匿名函数,即没有名称的函数。它只是一个表达式,函数体比def简单很多。当我们需要创建一个函数来执行单个操作并且可以在一行中编写时,就可以用到匿名函数了。

Lamdba的主体是一个表达式,而不是一个代码块。仅仅能在lambda表达式中封装有限的逻辑进去。

利用Lamdba函数,往往可以将代码简化许多。

(2)Map函数

会将一个函数映射到一个输入列表的所有元素上,比如我们先创建了一个函数来返回一个大写的输入单词,然后将此函数应有到列表colors中的所有元素。

我们还可以使用匿名函数lamdba来配合map函数,这样可以更加精简。

(3)Reduce函数

当需要对一个列表进行一些计算并返回结果时,reduce()是个非常有用的函数。举个例子,当需要计算一个整数列表所有元素的乘积时,即可使用reduce函数实现。

它与函数的最大的区别就是,reduce()里的映射函数(function)接收两个参数,而map接收一个参数。

(4)enumerate函数

用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在for循环当中。

它的两个参数,一个是序列、迭代器或其他支持迭代对象;另一个是下标起始位置,默认情况从0开始,也可以自定义计数器的起始编号。

(5)Zip函数

用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表

当我们使用zip()函数时,如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同。


文章名称:python存储函数 python保存函数
文章分享:http://pwwzsj.com/article/highci.html