帮助基本的Python函数

时间:2009-09-11 15:15:29

标签: python

我有一个连接数据库的功能。此代码有效:

def connect():
    return MySQLdb.connect("example.com", "username", "password", "database")

但这不是:

def connect():
    host = "example.com"
    user = "username"
    pass = "password"
    base = "database"
    return MySQLdb.connect(host, user, pass, base)

为什么会这样?

1 个答案:

答案 0 :(得分:8)

pass是保留关键字。

选择不同的变量名称,您的代码应该可以正常工作 也许是这样的:

def connect():
   _host = "example.com"
   _user = "username"
   _pass = "password"
   _base = "database"
   return MySQLdb.connect(_host, _user, _pass, _base)