Simple pattern for global connection or config

Hi there. I am currently writing an application which speaks to ActiveDirectory with the LDAP3 library. I would like to establish a single connection to the server, and then have all the other modules be able to do various operations on the server, using that persistent connection. Would it be best to use a Singleton class, or to pass a connection object around? I am unsure of how best to approach this, and am getting confused when looking at the different ways it could be done.

At the moment I have a main module, which creates a connection:

LDAPConnection = ldap3.Connection(LDAPServer, user=config.LDAPUser, passsord=config.LDAPPassword, auto_bind=True)

In later code, I wish to user a function from

from handlers import processFile

The code in handlers/processFile() needs to be able to use the LDAPConnection which was created in the main module of the application. How to do this?

Many thanks

There are very few good reasons for singleton classes. Python has three (None, Ellipsis and NotImplemented) but they are effectively used as constant values: they have no state and very little behaviour.

Many developers consider that singletons are an anti-pattern to be avoided. Apart from the “singleton as a constant stateless value”, I agree with that.

Without looking deep into your code design, I would probably just pass the connection object around. That makes it easy to extend your code to use multiple connections, or connections to different ActiveDirectory servers (is that a thing? I’m not a Windows admin), and most importantly, it makes it easier to test functions in isolation by using a mock connection object.