“Magnolia”通过精心收集,向本站投稿了6篇Python中实现结构相似的函数调用方法,下面是小编为大家整理后的Python中实现结构相似的函数调用方法,仅供参考,大家一起来看看吧。
- 目录
篇1:Python中实现结构相似的函数调用方法
这篇文章主要介绍了Python中实现结构相似的函数调用方法,本文讲解使用dict和lambda结合实现结构相似的函数调用,给出了不带参数和带参数的实例,需要的朋友可以参考下
python的dict用起来很方便,可以自定义key值,并通过下标访问,示例如下:
代码如下:
>>>d = {‘key1‘:‘value1‘,
... ‘key2‘:‘value2‘,
... ‘key3‘:‘value3‘}
>>>print d[‘key2‘]
value2
>>>
lambda表达式也是很实用的东东,示例如下:
代码如下:
>>>f = lambda x : x**2
>>>print f(2)
4
>>>
两者结合可以实现结构相似的函数调用,使用起来很方便,示例如下:
示例一:不带参数
代码如下:
#! /usr/bin/python
msgCtrl = “1 : pausen2 : stopn3 : restartnother to quitn”
ctrlMap = {
‘1‘: lambda : doPause,
‘2‘: lambda : doStop(),
‘3‘: lambda : doRestart()}
def doPause():
print ‘do pause‘
def doStop():
print ‘do stop‘
def doRestart():
print ‘do restart‘
if __name__ == ‘__main__‘:
while True:
print msgCtrl
cmdCtrl = raw_input(‘Input : ‘)
if not ctrlMap.has_key(cmdCtrl):break
ctrlMap[cmdCtrl]()
示例二:带参数
代码如下:
#! /usr/bin/python
msgCtrl = “1 : +n2 : -n3 : *nother to quitn”
ctrlMap = {
‘1‘: lambda x,y : x+y,
‘2‘: lambda x,y : x-y,
‘3‘: lambda x,y : x*y}
if __name__ == ‘__main__‘:
while True:
print msgCtrl
cmdCtrl = raw_input(‘Input : ‘)
if not ctrlMap.has_key(cmdCtrl):break
print ctrlMap[cmdCtrl](10,2),“n”
篇2:python使用multiprocessing模块实现带回调函数的异步调用方法
作者:work24 字体:[增加 减小] 类型:转载
这篇文章主要介绍了python使用multiprocessing模块实现带回调函数的异步调用方法,实例分析了multiprocessing模块异步调用的相关使用技巧,需要的朋友可以参考下
本文实例讲述了python使用multiprocessing模块实现带回调函数的异步调用方法,分享给大家供大家参考。具体分析如下:
multipressing模块是python 2.6版本加入的,通过这个模块可以轻松实现异步调用
from multiprocessing import Pooldef f(x): return x*xif __name__ == ‘__main__‘: pool = Pool(processes=1) # Start a worker processes. result = pool.apply_async(f, [10], callback) # Evaluate “f(10)” asynchronously calling callback when finished.
希望本文所述对大家的Python程序设计有所帮助,
篇3:python中实现php的vardump函数功能
这篇文章主要介绍了python中实现php的var_dump函数功能,var_dump函数在PHP中调试时非常实用,本文介绍在Python中实现这个函数,需要的朋友可以参考下
最近在做python的web开发(原谅我的多变,好东西总想都学着,。。node.js也是),不过过程中总遇到些问题,不管是web.py还是django,开发起来确实没用php方便,毕竟存在的时间比较短,很多不完善的地方。
比如我在调试php中最常用的函数,var_dump,在python里找不到合适的替代函数。php中var_dump是一个特别有用的函数,它可以输出任何变量的值,不管你是一个对象还是一个数组,或者只是一个数。它总能用友好的方式输出,我调试的时候经常会需要看某位置的变量信息,调用它就很方便:
但是开发python的时候就没有太好的替代方案。
之前想到repr,但这个函数只是调用了对象中的__str__,和直接print obj没啥区别。print是打印它,而repr是将其作为值返回。如果对象所属的类没有定义__str__这个函数,那么返回的就会是难看的一串字符。
后来又想到了vars 函数,vars函数是python的内建函数,专门用来输出一个对象的内部信息。但这个对象所属的类中必须有__dict__函数。一般的类都有这个dict,但像[]和{}等对象就不存在这个dict,这样调用vars函数就会抛出一个异常:
代码如下:
Traceback (most recent call last):
File “
TypeError: vars argument must have __dict__ attribute
所以后来几经寻找,找到一个个比较好,功能能够与var_dump类似的函数如下:
代码如下:
def dump(obj):
‘‘‘return a printable representation of an object for debugging‘‘‘
newobj=obj
if ‘__dict__‘ in dir(obj):
newobj=obj.__dict__
if ‘ object at ‘ in str(obj) and not newobj.has_key(‘__type__‘):
newobj[‘__type__‘]=str(obj)
for attr in newobj:
newobj[attr]=dump(newobj[attr])
return newobj
这是使用方式:
代码如下:
class stdClass(object): pass
bj=stdClass()
obj.int=1
obj.tup=(1,2,3,4)
obj.dict={‘a‘:1,‘b‘:2, ‘c‘:3, ‘more‘:{‘z‘:26,‘y‘:25}}
obj.list=[1,2,3,‘a‘,‘b‘,‘c‘,[1,2,3,4]]
obj.subObj=stdClass()
obj.subObj.value=‘foobar‘
from pprint import pprint
pprint(dump(obj))
最后输出是:
代码如下:
{‘__type__‘: ‘<__main__.stdClass object at 0x2b126000b890>‘,
‘dict‘: {‘a‘: 1, ‘c‘: 3, ‘b‘: 2, ‘more‘: {‘y‘: 25, ‘z‘: 26}},
‘int‘: 1,
‘list‘: [1, 2, 3, ‘a‘, ‘b‘, ‘c‘, [1, 2, 3, 4]],
‘subObj‘: {‘__type__‘: ‘<__main__.stdClass object at 0x2b126000b8d0>‘,
‘value‘: ‘foobar‘},
‘tup‘: (1, 2, 3, 4)}
然后github有个开源的module,可以参考:github.com/sha256/python-var-dump
说一下pprint这个函数,他是一个人性化输出的函数,会将要输出的内容用程序员喜欢的方式输出在屏幕上,
参阅这篇文章比较好理解:www.jb51.net/article/60143.htm
篇4:python中定义结构体的方法
最近更 新
python搭建简易服务器分析与实现
Python 字符串操作实现代码(截取/替换/查
Python 字符串定义
Python程序设计入门(2)变量类型简介
从零学Python之入门(三)序列
python中使用enumerate函数遍历元素实例
python实现目录树生成示例
python获取豆瓣电影简介代码分享
python中的__init__ 、__new__、__call__
Python3.x和Python2.x的区别介绍
热 点 排 行
Python入门教程 超详细1小时学会
python 中文乱码问题深入分析
比较详细Python正则表达式操作指
Python字符串的encode与decode研
Python open读写文件实现脚本
Python enumerate遍历数组示例应
Python 深入理解yield
Python+Django在windows下的开发
python 文件和路径操作函数小结
python 字符串split的用法分享
篇5:Python中针对函数处理的特殊方法
最近更 新
Python ORM框架SQLAlchemy学习笔记之安装
python ElementTree 基本读操作示例
python命令行参数sys.argv使用示例
pycharm 使用心得(八)如何调用另一文件
python实现问号表达式(?)的方法
Python splitlines使用技巧
python3.3使用tkinter开发猜数字游戏示例
复制粘贴功能的Python程序
python 简易计算器程序,代码就几行
Python 条件判断的缩写方法
热 点 排 行
Python入门教程 超详细1小时学会
python 中文乱码问题深入分析
比较详细Python正则表达式操作指
Python字符串的encode与decode研
Python open读写文件实现脚本
Python enumerate遍历数组示例应
Python 深入理解yield
Python+Django在windows下的开发
python 文件和路径操作函数小结
python 字符串split的用法分享
篇6:Python中实现三目运算的方法
这篇文章主要介绍了Python中实现三目运算的方法,本文用and/or运算符模拟实现三目运算,需要的朋友可以参考下
C语言中三目运算符
代码如下:
expression ?expr1:expr2; //expression 为真则取表达式expr1的值,否则取expr2的值
python三目实现方法:
(1) expr=判断表达式 and expr1 or expr2
判断表达式为真,此时如果expr1为真则expr=expr1,为假则变成False or expr2,expr=expr2.
判断表达式为假,则演变成False or expr2,此时expr=expr2.
只要表达式expr1,expr2都为真,上述等式就等同于C中的
expr=判断表达式 ?expr1:expr2;
(2)min(x,y)=(x (3)min(x,y=x if x
★ Python在Windows和在Linux下调用动态链接库的教程
Python中实现结构相似的函数调用方法(共6篇)




