现在的位置: 首页 > 综合 > 正文

python:大量参数如何传递

2012年09月10日 ⁄ 综合 ⁄ 共 492字 ⁄ 字号 评论关闭

一般来说函数参数的个数不宜过多,过多了以后函数将不容易理解和使用,在C里如果函数参数太多,那么会通过传结构指针来减少函数参数

//参数定义
struct student s;
s.id = 1;
strcpy(s.name,"hello");
s.age = 20;
s.sex = 1;

//函数调用
register(&s);

//函数定义
int register(struct student *p)
{
//写入数据库
}

在python里如何传递呢,尤其是我不想用class来传递信息,使用字典,而且在webpy框架里可以直接使用字典名进行参数化SQL操作

s = {
"id":1,
"name":"hello",
age:20
sex:1
}

register(s)

def register(student):
import web #webpy框架
db = web.database(dbn='mysql', user='root', pw='123xyz', db='smg')
db.query("insert into Student (id,name,age,sex) values($id,$name,$age,$sex)",vars=student)

抱歉!评论已关闭.