网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

python如何存储函数 python如何保存文件

Python如何将生成的代码/函数保存进文件并导入

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

创新互联公司-专业网站定制、快速模板网站建设、高性价比十堰郧阳网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式十堰郧阳网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖十堰郧阳地区。费用合理售后完善,10年实体公司更值得信赖。

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文件中,通过模块导入。自定义模块采用import语句来进行引入,其操作步骤是先导入模块,再调用模块中包含的函数,可将自定义模块放入当前目录,便于解释器路径搜索。


分享名称:python如何存储函数 python如何保存文件
网站URL:http://bjjierui.cn/article/hhchhs.html

其他资讯