How to operate on the same instance of a class?

I need your help on this one problem I have.

In my application I have a lot of modules that exist in multiple packages. These modules have 1 class definition in each module. The instances need to operate on the same data. Currently, I am doing this (as a hack):

# module1.py
class MyClass1:
    pass

my_class_1 = MyClass1()


# module2.py
from module1 import my_class_1
class MyClass2:
    ...
    # Operate on my_class_1
    ...

my_class_2 = MyClass2()

# module3.py
from module1 import my_class_1
class MyClass3:
    ...
    # Also operate on my_class_1
    ...

my_class_3 = MyClass3()

This code is an example, but you can see what I mean. I import an instance rather than a class as to then operate on the same instance. But I think this is an ugly hack. What could I do to solve my problem operating on the same instance in a Pythonic way?

Perhaps you need to use the Borg pattern: Avoiding the Singleton Design Pattern with the Borg Idiom - Python Cookbook [Book]

There’s actually a quite simple solution to use the same instance of a class in Python. Here’s a demo example:

#### module1.py ####

class MyClass1:
    ...

# Create an instance of the class in the same module!
my_class_1 = MyClass1()


#############################################################################
#############################################################################


#### module2.py ####

# Here import the instance (not the class!) from module1
from module1 import my_class_1

class MyClass2:
    # Do stuff to my_class_1
    ...


#############################################################################
#############################################################################


#### module3.py ####

# Here also import the instance (not the class!) from module1
from module1 import my_class_1

class MyClass3:
    # Also do stuff to my_class_1
    ...

In the example above, no matter which module (whether module2.py or module3.py) changes data of the my_class_1 instance, the changed data is reflected in both modules synchronously.

I want to know whether this is considered Pythonic.

Hi Boštjan,

Your first problem is that you are writing Java code in Python. It is
not compulsory or recommended to use the Java style “one class per
file”. If your classes naturally go together, then feel free to put them
in the same file.

The second problem is that this sounds like you are using the God Object
anti-pattern. If all of your classes need access to the same object,
that probably means that your code is too highly coupled with too much
depending on one single object.

If neither of these things are the case, and you really do need to keep
the classes in separate modules, and they all do have a dependency on
that one single object, then there is nothing wrong with what you do in
your example code:

  • initiate your one special instance in the first module;

  • all the other modules use from module1 import my_class_1 to
    get access to it.

This is fine, and it is not a “hack”.

Just remember that by doing that, all your modules are accessing a
single namespace, my_class_1, and sharing access to all that object’s
attributes (variables). If they all have write access as well as read
access, this is no different from using global variables, only less
convenient. So all the bad things about globals apply to your
my_class_1 object (and the good things too, only less convenient).

http://gamedevwithoutacause.com/?p=210

Thank you, @steven.daprano, this was very informative.