MySQLdb trouble

Thomas Bartkus thomasbartkus at comcast.net
Wed May 10 11:10:24 EDT 2006


"John Salerno" <johnjsal at NOSPAMgmail.com> wrote in message
news:Xnm8g.2076$No6.45677 at news.tufts.edu...
> Nicolay A. Vasiliev wrote:
>
> > def loc_connect_db():
> >    """
> >    The DB connection subroutine
> >    """
> >    db = MySQLdb.connect(host = "localhost",
> >                        user = "root",
> >                        passwd="mysql",
> >                        db="some_db")
> >    return db
>
> Hi. Sorry I can't help, but I'm interested in learning about mysqldb and
> I was wondering why you chose to wrap the 'connect' function inside your
> own function.

    > Does this accomplish something that I'm not seeing?

Probably!

1) His code body will be less likely to cause migrane headaches when he
tries to read and interpret what he did a year from now.  If you are trying
to figure out what is going on with the logic, user names and passwords can
be so much chaff your brain needs to wade through in order to get to the
central idea.

2) The coder won't have to repeat himself if he needs to re-open the
databases.
He can just call his less comples loc_connect_db() function.

And know that #1 is a sufficient reason even if #2 doesn't apply!

   > Couldn't you just use the body of the loc_connect_db() function as your
   > actual code in order to get 'db'?

Yes you could!
It's really a matter of style and preference.  Some programmers (myself
included!)  prefer many, very short and simple functions over fewer function
with longer blocks of more complex code.

It's hard to make a mistake by having too many short and simple functions.

And much too easy to make them when you have too few ;-)
Thomas Bartkus

.





More information about the Python-list mailing list