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?

SilentGhost
322k67 gold badges312 silver badges294 bronze badges
asked Sep 11, 2009 at 15:15
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)
answered Sep 11, 2009 at 15:17
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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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