4

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?

1
  • 1
    up vote for using the "beginner" tag. Commented Sep 11, 2009 at 16:03

1 Answer 1

8

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Haha, didn't realize. Thanks!
Ah, got tripped up by syntax highlighting. Corrected.
the convention is to append _ to the keyword. When you prepend, it means it's a private variable.
@Bastien True. Considering the placement of the vars, I assumed private.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.