易截截图软件、单文件、免安装、纯绿色、仅160KB

Python 中的函数调用(类似c中的使用函数入口地址)

def login():
    print 'login'
def logout():
    print 'logout'
controllers = {
'in': login,                                                ##(类似c中的:使用函数入口地址作为字典的值)
'out':logout,
}
def dispatcher(url, controllers):
    func = controllers.get(url, None)         ##(类似c中的:将函数入口指针赋给func)
    if func:
        func()                                              ##(类似c中的:这里通过函数入口指针调用函数)
    else:
        print 'Wrong url!'
dispatcher('in',controllers)
dispatcher('out',controllers)
dispatcher('23',controllers)
结果 :
login
logout
Wrong url!
 ---------------------------------------------------------------------------------------------------------
Python中函数是个对象,所以也是和其他对象一样的使用。
def f1():   ##声明了一个函数对象
    pass
a=f1        ##把函数对象f1赋给a
print a
print f1    ##结果相同 (都指向同一个地址)
f1()         ##调用函数对象
a()          ##调用函数对象


相关文档:

python中获得某月有多少天的函数

  偶然需要用到这样一个函数,在Delphi中,有现成的函数可以调用!在python中,我找了半天都还没找到,最后自己写了一个函数如下:
def dayOfMonth(date):
if date.month == 12:
return 31
else:
return (date.replace(month=date.month+1, day=1) - datetime.timedelta(days=1)).day
......

python中的列表排序操作

 
 
 
Python lists have a built-in sort() method that modifies the list in-place and a sorted() built-in function that builds a new sorted list from an iterable.
There are many ways to use them to sort data and there doesn't appear to be a single, central place in the various man ......

Python内建函数之——filter,map,reduce

     在讲述filter,map和reduce之前,首先介绍一下匿名函数lambda。
     lambda的使用方法如下:lambda [arg1[,arg2,arg3,...,argn]] : expression
     例如:
     >>> add = lambda x,y : x + y
>>> add ......

Python笔记(10)

Python中的异常
 
当你的程序中出现某些异常的状况的时候,异常就发生了。
 
一.处理异常
我们可以使用try..except语句来处理异常。我们把通常的语句放在try-块中,而把我们的错误处理语句放在except-块中。
例如:
#!/usr/bin/python
# Filename: try_except.py
import sys
try:
s = raw_input('E ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号