# Simple pattern for global connection or config

**URL:** <https://discuss.python.org/t/simple-pattern-for-global-connection-or-config/21518>\
**Category:** Python Help\
**Created:** [November 29, 2022, 3:21pm UTC](https://discuss.python.org/t/simple-pattern-for-global-connection-or-config/21518 "2022-11-29T15:21:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sugarat](https://avatars.discourse-cdn.com/v4/letter/s/87869e/32.png) [@Sugarat](https://discuss.python.org/u/Sugarat)\
**Post date:** [November 29, 2022, 3:21pm UTC](https://discuss.python.org/t/simple-pattern-for-global-connection-or-config/21518/1 "2022-11-29T15:21:29Z")

</div>

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:

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

```

In later code, I wish to user a function from handlers.py:

```auto
from handlers import processFile
processFile(incomingFile)

```

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

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [November 29, 2022, 10:49am UTC](https://discuss.python.org/t/simple-pattern-for-global-connection-or-config/21518/2 "2022-11-29T10:49:21Z")

</div>

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](https://www.contentful.com/blog/singleton-pattern-evil-or-misused/). 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.
