Grouping methods of a Python class I have a class with numerous methods, and so I would like to split these up into groups. This would make it easier for the user to find the method they are after. Here is a minimal example of this functionality.

class Greeter():

    def __init__(self, name):
        self.name = name
        self.spanish = Spanish(self)

   def generic(self):
       return f"Hello, {self.name}"

class Spanish():

        def __init__(self, greeter):
            self.grtr = greeter

        def hola(self):
            return f"Hola, {self.grtr.name}"

        def buen_dia(self):
            return f"Buenos días, {self.grtr.name}"

greeter = Greeter("Ann Example")
greeter.generic()
#&gt; "Hello, Ann Example"
greeter.spanish.hola()
#&gt; "Hola, Ann Example"


This feels like a hacky approach. Are there places people could see this failing? Is there a better solution in general?

EDIT

As suggested, here is a reduced example of my actual need for this functionality:

class Matrix():

    def __init__(self, m, n, data):
        # checks on m, n, data
        self.m = m
        self.n = n
        self.data = data
        self.decomposition = Decomposition(self)

    # many generic methods some returning more matrices
    def __add__(self, other):
        # add matrices
        return Matrix(m, n, data)

    # some methods will affect m, n, data
    def transpose(self):
        # set up new attributes
        self.m = m
        self.n = n
        self.data = data

class Decomposition():

    def __init__(self, mtrx):
        self.mtrx = mtrx

    # NB: eventually these might change m, n, data
    def QR(self):
        # do stuff for QR decomposition using self.mtrx
        return Q, R

    def LU(self):
        # do stuff for LU decomposition using self.mtrx
        return L, U
This is a review of the initial code that was posted. Unfortunately, I don't have time to revise it now for the new code.



Wanting to lessen the number of methods in a class is on its own a poor reason to split up a class into multiple. Classes can have many methods, and we can use IDE functionality and good documentation to make sense of a class.

Having many methods can suggest though that the class is doing too much, and that would be a more logical justification to split the class up. A better thing to think about is not the size of the class, but what the job of the class is, and if it's doing too many things.

That said, if you feel like it's justified to split it, I wouldn't do it as you have here. You've given the entire state of the Greeter object to Spanish. If Spanish begins using a lot of the state in Greeter, you now have two tightly coupled classes that will begin to break as you make changes to either.

Just give Spanish the information that it explicitly needs:

class Spanish:
    def __init__(self, name):
        self.name = name

    def hola(self):
        return f"Hola, {self.name}"

    def buen_dia(self):
        return f"Buenos días, {self.name}"


And

class Greeter:
    def __init__(self, name):
        self.name = name
        self.spanish = Spanish(name)  # And just pass the name
    . . .


This setup really doesn't make sense though. To get a greeting in Spanish, you need to access an object that greets in English? Even combined it doesn't make much sense. Having an object that greets in multiple languages seems odd. It would make more sense to have completely discrete classes that don't know about each other.

With a toy example like this though, it's really hard to make a concrete suggestion because the actual intent isn't clear. The textbox that I'm writing the answer in is also bugging out, so I'm going to submit this before I lose everything.