I have a function to connect to a database. This code works:
def connect():
return MySQLdb.connect("example.com", "username", "password", "database")
But this doesn't:
def connect():
host = "example.com"
user = "username"
pass = "password"
base = "database"
return MySQLdb.connect(host, user, pass, base)
Why so?
SilentGhost
322k67 gold badges312 silver badges294 bronze badges
asked Sep 11, 2009 at 15:15
user114060
-
1up vote for using the "beginner" tag.djangofan– djangofan2009年09月11日 16:03:49 +00:00Commented Sep 11, 2009 at 16:03
1 Answer 1
pass is a reserved keyword.
Pick different variable names and your code should work fine.
Maybe something like:
def connect():
_host = "example.com"
_user = "username"
_pass = "password"
_base = "database"
return MySQLdb.connect(_host, _user, _pass, _base)
answered Sep 11, 2009 at 15:17
Jon W
15.9k6 gold badges39 silver badges48 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Jon W
Ah, got tripped up by syntax highlighting. Corrected.
Bastien Léonard
the convention is to append
_ to the keyword. When you prepend, it means it's a private variable.Jon W
@Bastien True. Considering the placement of the vars, I assumed private.
lang-py