On 30/03/18 15:55, Glenn Schultz wrote: > I can create a connection as follows and it works
Well done, so you get a connection object back as a result. > but I think it is best to have a connection class that opens and closes. Why? You already have a connection object. Classes are created so that you can create instances. What would an instance of your Connection class offer that the API connection object does not? Creating classes introduces significant effort and overhead, if you don't need them don't build them. If the instances of your class don't contribute some new functionality what is their point? The more code you create the more likely that there is a bug. > model_prod_conn = pyodbc.connect( > driver = '{ODBC Driver 13 for SQL Server}', > server = 'foo', > database = 'foo', > username = 'foo', > password = 'foo', > trusted_connection = 'yes') > > > class modelingdb(object): > def __init__(self): > self._db_connection = pyodbc.connect( > driver = '{ODBC Driver 13 for SQL Server}', > server = 'foo', > database = 'foo', > username = 'foo', > password = 'foo', > trusted_connection = 'yes') > self._db_cur = self._db_connection.cursor() > > def __del__(self): > self._db_connection.close() There is a great talk somewhere on YouTube that provides guidance on when not to create a class. One of the cases is when you only have constructor and destructor. You may want to continue as an academic exercise in wrapping an API object. That's fair enough. But your current class offers nothing that the API does not. (The db_cur attribute doesn't really count since it's just a call to the connection object anyway. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor