资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

python存储函数 python对象存储

python 字典可以储存函数吗

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

目前创新互联已为超过千家的企业提供了网站建设、域名、虚拟主机绵阳服务器托管、企业网站设计、浮山网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

这时就可以使用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如何将生成的代码/函数保存进文件并导入

Python具备动态导入module并且执行其中代码的能力,所以你只要import你保存的东西就可以,无需中断你当前的代码执行流。

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对象存储
网站链接:http://cdkjz.cn/article/hhgedd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220