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() ##调用函数对象
相关文档:
偶然需要用到这样一个函数,在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
......
Delphi 与 C/C++ 数据类型对照表
Delphi数据类型C/C++
ShorInt
8位有符号整数
char
Byte
8位无符号整数
BYTE,unsigned short
SmallInt
16位有符号整数
short
Word
16位无符号整数
unsigned short
Integer,LongInt
32位有符号整数
int,long
Cardinal,LongWord/DWORD
32位无符号整数
unsigned long
Int6 ......
客户给一堆图片要传到后台,图片太大了,上百张图用photoshop改太慢,就想到用python写个简单的批处理。功能简单就是把原图按比例缩小,代码更简单 20多行。
# -*- coding: cp936 -*-
import Image
import glob, os
#图片批处理
def timage():
for files in glob.glob('D:\\1\\*.JPG'):
filepath,filena ......
模块
一.简介
模块基本上就是一个包含了所有你定义的函数和变量的文件。为了在其他程序中重用模块,模块的文件名必须以.py为扩展名。
例如:
#!/usr/bin/python
# Filename: using_sys.py
import sys
print 'The command line arguments are:'
for i in sys.argv:
print i
print '\n ......